简体   繁体   中英

Count from multiple tables

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;

The problem is your counting after the LEFT JOIN . In this case, COUNT(*) always returns a value of at least 1, because it is counting rows. You need to count matches, so count a column in the second table:

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;
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM