我正在做各种排名表,需要从查询中找到特定的行位置。
例如: SELECT name FROM players ORDER BY points DESC
如何使用“John”的name
字段或任何其他字段获取记录的位置?
所以我希望结果给我一个数字('John'的等级)
答案 0 :(得分:1)
SET @rank=0;
SELECT @rank:=@rank+1 AS rank, name
FROM players ORDER BY points DESC
或
SELECT @rn:=@rn+1 AS rank, name
FROM (
SELECT name
FROM players ORDER BY points DESC
) t1, (SELECT @rn:=0) t2;