SQL - 获取具有计数的图像,并计算我喜欢的图像

时间:2016-05-02 19:58:34

标签: mysql

我遇到了一个查询来获取图片,还有他们喜欢的图片,但我的每张图片都是喜欢的,我的实际查询是:

SELECT images.idimage, count( likes.idlike ) likes, count( likes2.idlike ) mylikes
FROM images
LEFT JOIN likes ON images.idimage = likes.idimage
LEFT JOIN likes likes2 ON images.idimage = likes2.idimage AND likes2.iduser =3
GROUP BY images.idimage

但它似乎不起作用,列mylikes让我完全喜欢图像,而不仅仅是我的。

对此有何帮助?

由于

1 个答案:

答案 0 :(得分:2)

You can do also this way 

SELECT images.idimage, count( *  ), sum(case when iduser=3 then  1 else 0 end) as mylikes
FROM images
LEFT JOIN likes ON images.idimage = likes.idimage
GROUP BY images.idimage