Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在做一个排序表,需要从查询中找到一个特定的行位置。
eg: SELECT name FROM player ORDER BY points DESC
我将如何获得带有name“约翰”或其他任何字段的记录位置?
name
所以我希望结果给我一个数字(“约翰”的排名)
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;