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 

count を使用すると、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 に答える