Is there any way I can get the actual row number from a query?
I want to be able to order a table called league_girl by a field called score; and return the username and the actual row position of that username.
I’m wanting to rank the users so i can tell where a particular user is, ie. Joe is position 100 out of 200, i.e.
User Score Row
Joe 100 1
Bob 50 2
Bill 10 3
I’ve seen a few solutions on here but I’ve tried most of them and none of them actually return the row number.
I have tried this:
SELECT position, username, score
FROM (SELECT @row := @row + 1 AS position, username, score
FROM league_girl GROUP BY username ORDER BY score DESC)
As derived
…but it doesn’t seem to return the row position.
Any ideas?
You may want to try the following:
The
JOIN (SELECT @curRow := 0)part allows the variable initialization without requiring a separateSETcommand.Test case:
Test query:
Result: