mysql查询获取特定记录

时间:2012-06-21 09:56:58

标签: mysql position

我正在做各种排名表,需要从查询中找到特定的行位置。

例如:     SELECT name FROM players ORDER BY points DESC

如何使用“John”的name字段或任何其他字段获取记录的位置?

所以我希望结果给我一个数字('John'的等级)

1 个答案:

答案 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;