mysql-sql语句-获取排名(包括是否并列排名)

本文详细介绍了在MySQL中如何使用SQL语句获取各种排名情况,包括排名前3、倒数3名、第2名、倒数第3名、前2-3名、前3和后3的记录,以及在有并列排名情况下的处理方法。同时,还展示了如何查询每个班级排名前2和倒数第1的记录,以及如何实现rank排名查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

举例数据表

如库中有表scores,表中已有数据如下:
在这里插入图片描述

score排名前3的记录

方法1. select * from scores order by score desc limit 3
注:不支持并列排名的情况

方法2. select * from scores where score>= (select max(score) from scores where score <(select max(score) from scores where score not in (select max(score) from scores))) order by score desc
注:支持并列排名的情况

score排名倒数3名的记录

方法1. select * from scores order by score limit 3
注:不支持并列排名的情况

方法2. select * from scores where score<= (select min(score) from scores where score >(select min(score) from scores where score not in (select min(score) from scores))) order by score
注:支持并列排名的情况

score排名第2的记录

方法1. select * from scores order by score desc limit 1,1
注:不支持并列排名的情况

方法2. select * from scores where score = (select max(score) from scores where score <(select max(score) from scores))
注:支持并列排名的情况

方法3. select * from scores where score in (select max(score) from scores where score not in (select max(score) from scores))
注:支持并列排名的情况

score排名倒数第3的记录

方法1. select * from scores order by score limit 2,1
注:不支持并列排名的情况

方法2. select * from scores where score= (select min(score) from scores where score >(select min(score) from scores where score not in (select min(score) from scores)))
注:支持并列排名的情况

score排名前2-3名的记录

方法1. select * from scores order by score desc limit 1,2
注:
1.不支持并列排名的情况
2.此语句语法为:select [fields] from [table] order by [field] [desc] limit 偏移量(从0开始),行数

方法2: select * from scores where score>= (select max(score) from scores where score <(select max(score) from scores where score not in (select max(score) from scores))) and score< (select max(score) from scores) order by score desc
注:支持并列排名的情况

score排名前3和后3的记录

select * from(select * from scores order by score limit 3) s1
union all
select * from (select * from scores order by score desc limit 3) s2

注:
1.一定要有别名,否则会报错(我猜是因为from后必须要跟表名,而直接select出来的数据不认为是表的一部分数据,因此为其起个名字作为表进行数据查询)
2.order by和union冲突,mysql不支持直接用
3.union 是会去掉重复的数据,而 union all则不去重
4.不支持并列排名情况,可以套用前2个例子的sql语句,将其union

如有下表class_scores,数据如下:

在这里插入图片描述

每个班级排名前2的记录

方法: select * from class_scores a where (select count(*) from class_scores b where a.class = b.class and a.score<b.score)<2 order by a.class,a.score desc
注:支持并列排名的情况

每个班级排名倒数第1的记录

方法: select * from class_scores a where (select count(*) from class_scores b where a.class = b.class and a.score>b.score)<1 order by a.class
注:支持并列排名的情况

每个班级排名第2的记录

方法: select * from class_scores a where (select count() from class_scores b where a.class = b.class and a.score<b.score)<2 and (select count() from class_scores b where a.class = b.class and a.score<b.score)>=1 order by a.class,a.score desc

注:
1.支持并列排名的情况
2. mysql不支持子查询中使用limit

MySQL中实现rank排名查询

看其他同学已经有一篇文章,总结的不错,有需要的可以看下。地址是:MySQL中实现rank排名查询

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值