2011-08-28 247 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