我有以下表格:users
,posts
和likes
我想收到“喜欢'”的总数。对于该帖子,如果它存在,也是like
的id。
我需要同桌的2个JOINS吗?或者是否有更有效的方式
SELECT p.id, u.username, p.message, count(DISTINCT l.id) AS likeCount, l2.id AS likedID
FROM posts p
INNER JOIN users u ON p.userID = u.id
LEFT JOIN likes l ON p.id = l.postID
LEFT JOIN likes l2 ON l2.userID = ? AND l2.postID = p.id
WHERE p.status = 0
由于