0

我正在做一个排序表,需要从查询中找到一个特定的行位置。

eg: SELECT name FROM player ORDER BY points DESC

我将如何获得带有name“约翰”或其他任何字段的记录位置?

所以我希望结果给我一个数字(“约翰”的排名)

4

1 回答 1

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;
于 2012-06-21T09:59:35.077 回答