Reputation: 1209
I am using php with mysql and i want to get top 3 user whose rating is high I have following table
Table name - rating
id user_id rating
1 4 3
2 4 5
3 5 3
4 6 4
5 5 2
Now i want to get data according to ratings ( top rating users should be above with limit 3 users) How can i do this ?
Upvotes: 1
Views: 204
Reputation: 360
You should group by user_id, then calculate the rating average and finally sort and limit acordingly to your needs:
SELECT user_id, AVG(rating) as 'Average Rating'
FROM rating
GROUP BY user_id
ORDER BY 'Average Rating' DESC
LIMIT 3;
Upvotes: 1