1、单表查询
SELECT (@i:=@i+1) AS ranking, t.* FROM student t, (select @i:=0) AS j;
(@i:=@i+1)代表定义一个变量,每次叠加1;
(SELECT @i:=0) AS j 代表建立一个临时表,j是随便取的表名,但别名一定要的。
2、按年龄排序
SELECT (@i:=@i+1), p.* FROM (SELECT t2.name, t2.sex, t2.age, t2.score, t2.class FROM student t2 order by age desc)p, (SELECT @i:=0) k;