2011-08-28 121 views
0

我有一张带有注释的表格。这些评论与另一个问题表相关,通过一对多的关系,即1个问题到很多评论。现在,我想要一个包含最多评论数量的5个问题列表(当然是连续的)。所以,我的查询应该返回类似:需要MySql查询帮助

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

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

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; 
+0

谢谢。让我尝试。 – Blueboye