0

Im doing a rankings table of sorts, and need to find a specific rows position from a query.

eg: SELECT name FROM players ORDER BY points DESC

How would I get the position of the record with the name field of 'John', or any other?

So I want the result to give me one number (the rank of 'John')

user1022585
  • 12,121
  • 19
  • 53
  • 74

1 Answers1

1
SET @rank=0;
SELECT @rank:=@rank+1 AS rank, name 
  FROM players ORDER BY points DESC

or

SELECT @rn:=@rn+1 AS rank, name
FROM (
  SELECT name 
      FROM players ORDER BY points DESC
) t1, (SELECT @rn:=0) t2;
Romil Kumar Jain
  • 19,561
  • 8
  • 59
  • 90