时间:2021-07-01 10:21:17 帮助过:4人阅读
SELECT
CASE
WHEN @conditionA =-1 AND @conditionB = -1 THEN @dense_rank := 1 #判断是否第一个记录,是则排名为第一
WHEN @conditionA = conditionA AND @conditionB = conditionB THEN @dense_rank #判断当前记录是否与之前的一样,是则保持排名不变
ELSE @dense_rank := @dense_rank + 1 #其他情况,排名加1
END AS dense_rank,
@id_rank := @id_rank+1 AS i_rank, #递增序号字段
@conditionA := conditionA AS conditionA, #更新条件变量为当前记录的值
@conditionB := conditionB AS conditionB #更新条件变量为当前记录的值
FROM
(SELECT @id_rank:=0) i_rank, #初始化序号变量
(SELECT @dense_rank:=0) d, #初始化排名变量
(SELECT @conditionA:=-1) s, #初始化条件变量
(SELECT @conditionB:=-1) c, #初始化条件变量
table
order by conditionA desc, conditionB asc
MySQL排名SQL语句写法
标签: