Mysql - How to get a row number after Order by?
Row number after order by
SELECT ( @rank:=@rank + 1) AS rank, m.* from
(
SELECT a.p_id, a.userid
FROM (SELECT @rank := 0) r, point_table a
ORDER BY a.points DESC
) m
This answer should work for you:
SET @rank=0;
SELECT @rank:=@rank+1 AS rank, p_id FROM point_table ORDER BY points DESC;
Update: You might also want to consider to calculate the rank when updating the points and saving it to an additional column in the same table. That way you can also select a single user and know his rank. It depends on your use cases what makes more sense and performs better.
Update: The final solution we worked out in the comments looked like this:
SELECT
rank, p_id
FROM
(SELECT
@rank:=@rank+1 AS rank, p_id, userid
FROM
point_table, (SELECT @rank := 0) r
ORDER BY points DESC
) t
WHERE userid = intval($sessionuserid);