0

我只想计算所有子评论以及所有属于画廊的图像中的所有喜欢。

SELECT Count(Message.id) AS subCount, 
       Count(likes.id), 
       Image.id, 
       Image.date_created, 
       Image.likes, 
       Image.path 
FROM   images AS Image 
       LEFT JOIN messages AS Message 
              ON ( Message.object_id = Image.id 
                   AND Message.msg_type = 16 ) 
       LEFT JOIN likes AS Likes 
              ON ( likes.object_id = Image.id 
                   AND likes.object_type = 3 ) 
WHERE  Image.gallery_id = 31 

当我使用计数时,它只返回 1 行。有任何想法吗?

4

1 回答 1

2

你需要一个GROUP BY

SELECT Count(Message.id) AS subCount, 
       Count(likes.id), 
       Image.id, 
       Image.date_created, 
       Image.likes, 
       Image.path 
FROM   images AS Image 
       LEFT JOIN messages AS Message 
              ON ( Message.object_id = Image.id 
                   AND Message.msg_type = 16 ) 
       LEFT JOIN likes AS Likes 
              ON ( likes.object_id = Image.id 
                   AND likes.object_type = 3 ) 
WHERE  Image.gallery_id = 31 
GROUP  BY Image.id, 
          Image.date_created, 
          Image.likes, 
          Image.path 
于 2013-04-17T16:13:19.640 回答