开发者

MySql Query Help needed

I have a table with comments. These comments are related to another table of questions, through 1-to-many relation, i.e. 1 question to many comments. Now, I want a list of 5 questions with the maximum number of comment counts (in succession of course). So, my query should return something like:

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

Can I achieve this through 1 query or multiple ones? And how?


This query gets the data you need. You can handle the output formatting as desired.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜