amit
amit

Reputation: 1209

How to get top 3 users according to rating

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

Answers (2)

Fikret Basic
Fikret Basic

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

Bigfash45
Bigfash45

Reputation: 21

SELECT * FROM rating ORDER BY rating DESC LIMIT 3

Upvotes: 2

Related Questions