I have 3 tables
comments(id,question_id, user_Id) // here the user_id is of the user who has asked the question
questions(id,q_desc, user_id)
users(id, name, rank)
A user can ask question and can comment on the questions.
I need a report in which I want to display every question with at most 3 top ranked user, who have commented on it, but the user who has asked the question should not be in the report for that particular question, but he too has the privilege to comment on his question.
EDited:::
Select * from comments inner join users(comments.user_id=u.id) group by question_id order by user.rank desc
It’s messy but it works:
EDIT: This produces the same results and is more succinct:
These solutions also account for users that have posted more than one comment in the same question.
See both solutions in action at SQLFiddle