我正在尝试创建一个sql查询,该查询将返回imageId,此图像所拥有的总数,以及当前用户是否喜欢该图像的布尔值。
到目前为止我所拥有的是:
Select i.imageId, count(l.likeId) as numOfLikes
FROM Images as i LEFT JOIN Likes as l USING (imageID)
WHERE i.userId = '16'
GROUP BY i.imageId
ORDER BY i.dateCreated DESC
这对于imageIds和每张图片的喜欢总数非常有用。我不确定我是如何确定用户是否喜欢同一查询中的图像。
提前致谢!
答案 0 :(得分:1)
这样的事情应该有效。
Select i.imageId, count(l.likeId) as numOfLikes
, count(likes2.*) LikesByUser
FROM Images as i LEFT JOIN Likes as l USING (imageID)
left join likes likes2 using (imageID, UserId)
WHERE i.userId = '16'
GROUP BY i.imageId
ORDER BY i.dateCreated DESC
LikesByUser应为0或1。