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
  • 13,061
  • 21
  • 55
  • 75

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
  • 20,239
  • 9
  • 63
  • 92