需要MySql查询帮助

时间:2011-08-28 03:03:31

标签: mysql sql aggregate-functions

我有一张评论表。这些评论与另一个问题表相关,通过一对多关系,即1个问题到多个评论。现在,我想要一个包含最多评论数量的5个问题的列表(当然是连续的)。所以,我的查询应该返回如下内容:

Question Id:4 with 30 comments
Question Id:2 with 27 comments
Question Id:11 with 22 comments
Question Id:5 with 15 comments
Question Id:14 with 10 comments

我可以通过1个查询或多个查询实现此目的吗?如何?

1 个答案:

答案 0 :(得分:1)

此查询获取您需要的数据。您可以根据需要处理输出格式。

select questionid, count(commentid) as commentcount
from question q
inner join comment c on q.questionid = c.questionid
group by questionid
order by commentcount desc
limit 5;