低版本mysql 利用@变量实现row_number() over(partition by order by )排序功能

MySQL实现排名两种方式

一、窗口函数
MySQL 8.0版本用窗口函数就可以实现排名,有三种方式,对相同值的处理不同:

  • row_number():不产生相同的记录,没有序号间隔(值一致,排名不一致)

  • rank():产生相同的记录,有序号间隔(值一致,排名一致,名次占位)

  • dense_rank():产生相同记录,没有序号间隔(值一致,排名一致,名次不占位)
    (由于未安装8.0版本mysql,下面用oracle进行测试讲解)

(一)准备测试数据

with scores_tab as (
select '1班' as class_name,'01' as student_id ,100 as score from dual union all
select '1班' as class_name,'02' as student_id ,90  as score from dual union all
select '1班' as class_name,'03' as student_id ,83  as score from dual union all
select '1班' as class_name,'04' as student_id ,77  as score from dual union all
select '1班' as class_name,'05' as student_id ,77  as score from dual union all
select '1班' as class_name,'06' as student_id ,60  as score from dual union all
select '2班' as class_name,'01' as student_id ,97  as score from dual union all
select '2班' as class_name,'02' as student_id ,88  as score from dual union all
select '2班' as class_name,'03' as student_id ,60  as score from dual union all
select '2班' as class_name,'04' as student_id ,54  as score from dual union all
select '2班' as class_name,'05' as student_id ,33  as score from dual  )

select * from scores_tab

表数据如下:
在这里插入图片描述
(二)测试讲解
1、row_number():
1)简单排序

with scores_tab as (
select '1班' as class_name,'01' as student_id ,100 as score from dual union all
select '1班' as class_name,'02' as student_id ,90  as score from dual union all
select '1班' as class_name,'03' as student_id ,83  as score from dual union all
select '1班' as class_name,'04' as student_id ,77  as score from dual union all
select '1班' as class_name,'05' as student_id ,77  as score from dual union all
select '1班' as class_name,'06' as student_id ,60  as score from dual union all
select '2班' as class_name,'01' as student_id ,97  as score from dual union all
select '2班' as class_name,'02' as student_id ,88  as score from dual union all
select '2班' as class_name,'03' as student_id ,60  as score from dual union all
select '2班' as class_name,'04' as student_id 
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值