2011-04-05 32 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

相關問題