Let's say I have this table:
ID Score
1 345
2 5
3 76
4 45
5 85
6 175
7 209
and this is my query:
SELECT * FROM posts ORDER BY Score DESC
How do I get the rank/position of each row?
Let's say I have this table:
ID Score
1 345
2 5
3 76
4 45
5 85
6 175
7 209
and this is my query:
SELECT * FROM posts ORDER BY Score DESC
How do I get the rank/position of each row?
You can use a technique like @OMG Ponies linked to above like this:
SELECT posts.*, @row:=@row+1 as 'rank' FROM posts, (SELECT @row:=0) r ORDER BY posts.Score DESC
If you run below code, you will see new column as Position
which is same as Rank
SELECT ID, Score,
(SELECT COUNT(distinct u2.Score) FROM myTable u2
WHERE
u2.Score > u1.Score) + 1 AS position FROM myTable u1
ORDER BY position
Let me know if you have any questions...