描述: 数据有可排序字段,需要将排序字段做出来
函数: 使用变量参数,循环赋值
案例: 根据数据排序,给与排名值
- 数据准备
CREATE TABLE test001 (student VARCHAR,score INT
);
INSERT INTO test001(student, score) VALUES ('张三', 59);
INSERT INTO test001(student, score) VALUES ('李四', 72);
INSERT INTO test001(student, score) VALUES ('王麻子', 99);
INSERT INTO test001(student, score) VALUES ('诀绝子', 42);
*执行结果
- 数据处理
selectt1.student 姓名,t1.score 分数,@rownum := @rownum+1 as 排名 -- 变量赋值
from test001 t1,(select @rownum :=0) t0 -- 变量集合,变量初始值
order by t1.score DESC -- 业务排序
*执行结果