根据使用mysql的用户获得最大的评论数量?

时间:2011-04-05 17:46:56

标签: mysql

我有这4张桌子:

 posts{id,post,date}
    comment{id, user_id,post_id, comment, date}
    tag_post{tag_id,post_id}
    users{user_id, email,pwd,username}

我想进行这个复杂的查询,我希望从某个主题获得最大数量的评论者(用户): 即。

  select the most commeneters(count) on posts that have been tagged with tag_id=39
LIMIT 5

谢谢:))

1 个答案:

答案 0 :(得分:1)

这样的事情:

select users.user_id, count(*) as nb_comments
from posts
    inner join tag_posts on tag_posts.post_id = posts.id
    inner join comment on comment.post_id = posts.id
    inner join users on users.user_id = comment.user_id
where tag_posts.tag_id = 39
group by users.user_id
order by count(*) desc
limit 5

它应该为您提供五个用户,这些用户对包含标记39的帖子发表评论最多。