慕丝7291255
您可能需要尝试以下方法:SELECT l.position, l.username, l.score, @curRow := @curRow + 1 AS row_numberFROM league_girl lJOIN (SELECT @curRow := 0) r;这个JOIN (SELECT @curRow := 0)部件允许变量初始化,而不需要单独的SET命令。测试用例:CREATE TABLE league_girl (position int, username varchar(10), score int);INSERT INTO league_girl VALUES (1, 'a', 10);INSERT INTO league_girl VALUES (2, 'b', 25);INSERT INTO league_girl VALUES (3, 'c', 75);INSERT INTO league_girl VALUES (4, 'd', 25);INSERT INTO league_girl VALUES (5, 'e', 55);INSERT INTO league_girl VALUES (6, 'f', 80);INSERT INTO league_girl VALUES (7, 'g', 15);测试查询:SELECT l.position, l.username, l.score, @curRow := @curRow + 1 AS row_numberFROM league_girl lJOIN (SELECT @curRow := 0) rWHERE l.score > 50;结果:+----------+----------+-------+------------+| position | username | score | row_number |+----------+----------+-------+------------+| 3 | c | 75 | 1 || 5 | e | 55 | 2 || 6 | f | 80 | 3 |+----------+----------+-------+------------+3 rows in set (0.00 sec)