有这三个表:
posts
posts_replies
likes
此查询返回的数据几乎没问题,但出于某种原因,帖子的回复数不准确。
SELECT posts.title, posts.num, posts.status, posts.category, posts.content, posts.member_num, COUNT( posts_replies.post_num ) AS count, COUNT( likes.comment_num ) AS likes_count
FROM posts_replies
INNER JOIN posts ON ( posts_replies.post_num = posts.num )
LEFT JOIN likes ON ( likes.comment_num = posts_replies.num )
WHERE posts.status =1
AND posts.access = 'Public'
GROUP BY posts.num
ORDER BY count DESC
LIMIT 50
这是我使用的计数:COUNT( posts_replies.post_num ) AS 计数
对此有什么建议吗?
谢谢
最佳答案
您的查询将针对链接到该回复的每个喜欢对每个回复计数一次。
为了只计算每个回复一次,替换
COUNT( posts_replies.post_num ) AS count
与
COUNT(DISTINCT posts_replies.num ) AS count
关于MySQL:有点复杂的查询返回错误的计数结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15185477/