pingpong
pingpong

Reputation: 1247

getting the maxium count of comments in terms of users using mysql?

i have this 4 tables:

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

i want to make this complex query, i want to get the maxiumum number of commenters(users) from a certain topic: i.e.

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

thanks :))

Upvotes: 0

Views: 36

Answers (1)

Pascal MARTIN
Pascal MARTIN

Reputation: 401142

What about something like this :

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

It should get you the five users who commented the most on posts that have the tag 39.

Upvotes: 1

Related Questions