Tiffany
Tiffany

Reputation: 237

Get value from MySQL table, show only top ten highest and top ten lowest

I have a table like this:

  Username    Rating
1 xxxxxxxx      -1
2 xxxxxxxx       2
3 xxxxxxxx       5
4 xxxxxxxx      -2
5 xxxxxxxx       4

(Imagine if the table was bigger and had more than 20 rows). I want to write a MySQL statement in PHP that will return two lists: a list of the top ten highest values ordered by largest first, and a list of the top ten lowest values, ordered lowest first. How would I go about this?

Upvotes: 1

Views: 3278

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

You order the result and use limit 10

select *
from mytable
order by rating
limit 10

or the top highest values

select *
from mytable
order by rating desc
limit 10

Upvotes: 2

Thomas Kelley
Thomas Kelley

Reputation: 10292

Ten highest:

SELECT Username, Rating FROM <Table> ORDER BY Rating DESC LIMIT 10;

Ten lowest:

SELECT Username, Rating FROM <Table> ORDER BY Rating ASC LIMIT 10;

Upvotes: 7

Related Questions