I've got this little problem: I want to organize a top10 list for my game using MySQL. Well, actually it already works, but I need to add one feature to it. Say, I got the table which contains a field called 'level' for example, there's 10 thousand players in a database, each of them has his own level. So, to create a top ten list I do this:
SELECT `values`.`level` FROM `values` ORDER BY `values`.`level` DESC LIMIT 10
It works fine and gives me 10 players with the highest level. But what if the player himself is not on top10 list? And he wants to know how much is he left to pass to get there. Let's represent it like this: he sees the score table showing 10 best players, and at the bottoms of the table sees his own record saying, e.g your position is 4023 Of course I can retreive all records from MySQL order them by level descending, and count users's position using a loop, but captain obvious is telling me it isn't the most rational approach :-)
I need the result look like this (only for 10 users, not 5)
+-------+------+--------+
| level | pos |user |
+-------+------+--------+
| 23 | 1 |Alex |
| 19 | 2 |John |
| 18 | 3 |James |
| 16 | 4 |Ashley |
| 15 | 5 |Casey |
| | | |
| 4 | 145 |You |
+-------+------+--------+
So, is there some way to do it in one query?
Thanks!