MySQL:有点复杂的查询返回计数结果错误

时间:2013-03-03 12:08:28

标签: mysql

有这三个表:

posts
posts_replies
likes

此查询返回的数据几乎没有问题,但由于某种原因,帖子上的回复COUNT不准确。

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计数

有关于此的任何建议吗?

谢谢

1 个答案:

答案 0 :(得分:1)

您的查询会为每个回复计算一次,就像链接到该回复一样。

为了只计算一次回复,请替换

COUNT( posts_replies.post_num ) AS count

COUNT(DISTINCT posts_replies.num ) AS count