2011-04-05 36 views
0

我有这样的4个表:根据使用mysql的用户获得最多的评论数?

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

我想使这个复杂的查询,我想从某个话题让评论者(用户)的maxiumum数: 即

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

感谢:))

回答

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 

它应该让你的5个用户评论最有帖子的标签39。

+0

wooooow让我试试它.. :)) – pingpong 2011-04-05 17:55:39

相关问题