If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Joining Two Select Statements

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-20-11, 02:58
sandeepck sandeepck is offline
Registered User
 
Join Date: Feb 2011
Posts: 3
Joining Two Select Statements

Hi,

I have two Select statements shown below. I want to join these both and should get the proper result of No of posts and No of Comments with respect to the particular user. Can anybody please help me on this

select
user1.userid,
CONCAT(user1.firstname,' ',user1.lastname) as UserName,
count(na.activity_master_id) NoOfPosts
from
user user1
left outer join network_activity na on na.created_by = user1.userid
where na.status = 'a'
and na.network_id = 10
and na.activity_master_id = 9
group by user1.userid


select
user1.userid,
CONCAT(user1.firstname,' ',user1.lastname) as UserName,
count(nc.comment_by) NoOfComments
from
user user1
left outer join network_comments nc on nc.comment_by = user1.userid
left outer join network_activity na on na.id = nc.activity_id
where na.network_id = 10
group by user1.userid

Thanks in Advance
Reply With Quote
  #2 (permalink)  
Old 06-20-11, 04:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT user1.userid
     , CONCAT(user1.firstname,' ',user1.lastname) AS UserName
     , COALESCE(p.NoOfPosts,0) AS NoOfPosts
     , COALESCE(c.NoOfComments,0) AS NoOfComments
  FROM user AS user1
LEFT OUTER 
  JOIN ( SELECT created_by
              , COUNT(*) AS NoOfPosts
           FROM network_activity 
          WHERE status = 'a'
            AND network_id = 10
            AND activity_master_id = 9
         GROUP
             BY created_by ) AS p
    ON p.created_by = user1.userid
LEFT OUTER 
  JOIN ( SELECT comment_by
              , COUNT(*) AS NoOfComments
           FROM network_comments AS nc
         INNER
           JOIN network_activity AS na
             ON na.id = nc.activity_id
            AND na.network_id = 10
         GROUP
             BY comment_by ) AS c
    ON c.comment_by = user1.userid
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-21-11, 00:29
sandeepck sandeepck is offline
Registered User
 
Join Date: Feb 2011
Posts: 3
Thanks for the reply and it works properly. But what if i want to display the Post and Comments count of the particular network_id related users?
Reply With Quote
  #4 (permalink)  
Old 06-21-11, 05:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by sandeepck View Post
But what if i want to display the Post and Comments count of the particular network_id related users?
sorry, i don't understand the question
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On