0

我将此查询作为我的主要查询,我使用成员表中的所有记录,并从评论和聊天框中选择列。

SELECT members.*, 
       a.commenter_id, 
       b.user_id, 
       a.comcount, 
       b.chatcount 
FROM   members 
       LEFT JOIN (SELECT commenter_id, 
                         Count(*) comCount 
                  FROM   comments 
                  GROUP  BY commenter_id) a 
              ON members.id = a.commenter_id 
       LEFT JOIN (SELECT user_id, 
                         Count(*) chatCount 
                  FROM   chat_box 
                  GROUP  BY user_id) b 
              ON members.id = b.user_id 
WHERE  members.id = '290' 

我想将此查询添加到上面

SELECT Count(friend_id) AS totFriend, 
       friend_id        AS fi, 
       logged_user_id   AS user, 
       friend_accepted  AS fa 
FROM   member_friends 
WHERE  logged_user_id = '1' 
       AND friend_id = '290' 

是否可以将其添加到混合中而不会导致任何错误?我自己尝试过,但我可能只是让自己陷入更深层次的问题。如果可能有人可以帮助我这样做,谢谢:)。

4

1 回答 1

0

如果您有办法将最后一个查询加入到您的members表中,那么您应该能够使用:

SELECT members.*, 
  a.commenter_id, 
  b.user_id, 
  a.comcount, 
  b.chatcount,
  c.totFriend
FROM   members 
LEFT JOIN 
(
  SELECT commenter_id, 
    Count(*) comCount 
  FROM   comments 
  GROUP  BY commenter_id
) a 
  ON members.id = a.commenter_id 
LEFT JOIN 
(
  SELECT user_id, 
    Count(*) chatCount 
  FROM   chat_box 
  GROUP  BY user_id
) b 
 ON members.id = b.user_id 
LEFT JOIN
(
  SELECT Count(friend_id) AS totFriend, 
       friend_id        AS fi, 
       logged_user_id   AS user, 
       friend_accepted  AS fa 
  FROM   member_friends 
  WHERE  logged_user_id = '1' 
   AND friend_id = '290'
) c
  on members.id = c.friend_id
WHERE  members.id = '290' 
于 2013-01-15T23:07:35.720 回答