Count from multiple tables

时间:2016-04-07 10:51:43

标签: mysql sql

I am trying to count result from multiple tables. I have picture and picture has comments, likes and dislikes. I want to merge all that in one table query where i will show all photos and count likes, comments, dislikes for that photo.

My photo table looks like this:

photo_id  owner_id  album_id  image_type  photo_name                            photo_ext  photo_size  photo_type  photo_description  date_uploaded        date_midified        photo_guid                            bg_x_position  bg_y_position  
--------  --------  --------  ----------  ------------------------------------  ---------  ----------  ----------  -----------------  -------------------  -------------------  ------------------------------------  -------------  ---------------
       2         1         5           0  27bda14cb0e30efb0eeef5688e6c0ef9.jpg  .jpg       42.6        jpeg        (NULL)             2016-02-09 15:45:20  2016-02-09 15:45:20  E8CFF5F0-F8FA-8AD7-E3B5-63EAFE81E1B6  (NULL)         (NULL)         
       3         1         5           0  8077f0d2104c35c8e612e24834f82ace.jpg  .jpg       34.52       jpeg        (NULL)             2016-02-09 15:49:44  2016-02-09 15:49:44  09B25F5C-0A9D-0EBC-DCE9-EE24E8ED4B6D  (NULL)         (NULL)         
       4         1         5           0  6c28b264470e7a7f2829ea5b7290cbba.jpg  .jpg       85.65       jpeg        (NULL)             2016-02-09 15:49:56  2016-02-09 15:49:56  9A5EF85E-F691-F42E-C20C-BCDC765BFA1B  (NULL)         (NULL)         

Like table:

like_id  item_id  account_id    rate  time                 host    
-------  -------  ----------  ------  -------------------  --------
    308      2             1       1  2016-03-18 13:45:16  (NULL)  
    309      3             1       2  2016-03-18 13:45:33  (NULL)  
    310      2             7       1  2016-03-18 14:23:49  (NULL)  

Comment table:

comment_id  item_id   content       account_id     time                 
-------     -------  ----------     ------------  -------------------
  308       262      Test comment      1          2016-03-18 13:45:16 

So result what i want to have is like this:

photo_id  owner_id  album_id  image_type  photo_name                            photo_ext  photo_size  photo_type  photo_description  date_uploaded        date_midified        photo_guid                            bg_x_position  bg_y_position   likes  dislikes   comments
--------  --------  --------  ----------  -------------------------------  ---------  ----------  ----------  -----------------  -------------------  -------------------   ----   ------   ------  ------------------------------------  -------------  --------------

So on existing table i want to add 3 new cells (likes, dislikes and comments) count. My query work good but count of comments is not correct becouse he show all picure has 1 comment but there is no comment.

SELECT * FROM (SELECT p.photo_id, p.photo_name, 
    IFNULL(SUM(l.rate = 1), 0) AS likes, 
    IFNULL(SUM(l.rate = 2), 0) AS dislikes

FROM pb_account_photos AS p
LEFT JOIN pb_account_likes AS l ON l.item_id = p.photo_id
WHERE p.owner_id = 1 GROUP BY p.photo_id) AS LIKES,
(SELECT COUNT(*) AS comments, p.photo_id
FROM pb_account_photos AS p
LEFT JOIN pb_account_comments AS c ON c.item_id = p.photo_id
WHERE p.owner_id = 1 GROUP BY p.photo_id) AS COMM WHERE COMM.photo_id=LIKES.photo_id;

2 个答案:

答案 0 :(得分:2)

问题是你在LEFT JOIN后计算。在这种情况下,COUNT(*)始终返回至少为1的值,因为它正在计算行数。您需要计算匹配项,因此请计算第二个表中的列:

SELECT likes.*, comm.comments
FROM (SELECT p.photo_id, p.photo_name, 
             COALESCE(SUM(l.rate = 1), 0) AS likes, 
             COALESCE(SUM(l.rate = 2), 0) AS dislikes
      FROM pb_account_photos p LEFT JOIN
           pb_account_likes l
           ON l.item_id = p.photo_id
      WHERE p.owner_id = 1
      GROUP BY p.photo_id
     ) likes JOIN
     (SELECT COUNT(C.item_id) AS comments, p.photo_id
      FROM pb_account_photos p LEFT JOIN
           pb_account_comments c
           ON c.item_id = p.photo_id
      WHERE p.owner_id = 1
      GROUP BY p.photo_id
     ) comm 
     ON comm.photo_id = likes.photo_id;

答案 1 :(得分:1)

SELECT p.*,
SUM(l.rate = 1) AS likes
SUM(l.rate = 2)  AS dislikes
SUM(IF(ISNULL(c.item_id), 0, 1)) AS comments
FROM pb_account_photos p
LEFT JOIN pb_account_likes AS l ON l.item_id = p.photo_id
LEFT JOIN pb_account_comments AS c ON c.item_id = p.photo_id
GROUP BY p.photo_id