MySQL分组汇总取前 N 条记录

score表:

CREATE TABLE `score` (
  `student_id` int(10) DEFAULT NULL,
  `class_id` int(10) DEFAULT NULL,
  `score` int(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

字段 student_id 学生 id ,class_id:班级 id ,score:分数 
数据准备:

insert into score values(1,1,100),(2,1,93),(3,1,89),(4,1,96),(5,2,98),(6,2,97),(7,2,90),(8,2,88),(9,1,96);

表结构如下:

mysql> select * from score;
+------------+----------+-------+
| student_id | class_id | score |
+------------+----------+-------+
|          1 |        1 |   100 |
|          2 |        1 |    93 |
|          3 |        1 |    89 |
|          4 |        1 |    96 |
|          5 |        2 |    98 |
|          6 |        2 |    97 |
|          7 |        2 |    90 |
|          8 |        2 |    88 |
|          9 |        1 |    96 |
+------------+----------+-------+
9 rows in set (0.00 sec)

1.取每个班级前两名的学生(包含并列第二名)

mysql> select * 
from score s1 
where (select count(0) from score s2 where s1.class_id = s2.class_id and s1.score < s2.score) < 2;
+------------+----------+-------+
| student_id | class_id | score |
+------------+----------+-------+
|          1 |        1 |   100 |
|          4 |        1 |    96 |
|          5 |        2 |    98 |
|          6 |        2 |    97 |
|          9 |        1 |    96 |
+------------+----------+-------+
5 rows in set (0.00 sec)

sql 解释:取表 s1的数据,这些数据中 class_id 和 s2 class_id相同的数据下,比 s1的 score 分数大的 s2的数据条目必须小于2

或者使用 left join 的方式:

mysql> select s1.* 
from score s1 
left join score s2 on s1.class_id = s2.class_id and s1.score<s2.score 
group by s1.class_id,s1.student_id,s1.score 
having count(s2.student_id)<2;
+------------+----------+-------+
| student_id | class_id | score |
+------------+----------+-------+
|          1 |        1 |   100 |
|          4 |        1 |    96 |
|          9 |        1 |    96 |
|          5 |        2 |    98 |
|          6 |        2 |    97 |
+------------+----------+-------+
5 rows in set (0.00 sec)

2.取学生分数数据且表示排名

mysql> select s1.*,(select count(0) + 1 from score s2 where s2.score > s1.score)rank from score s1;
+------------+----------+-------+------+
| student_id | class_id | score | rank |
+------------+----------+-------+------+
|          1 |        1 |   100 |    1 |
|          2 |        1 |    93 |    6 |
|          3 |        1 |    89 |    8 |
|          4 |        1 |    96 |    4 |
|          5 |        2 |    98 |    2 |
|          6 |        2 |    97 |    3 |
|          7 |        2 |    90 |    7 |
|          8 |        2 |    88 |    9 |
|          9 |        1 |    96 |    4 |
+------------+----------+-------+------+
9 rows in set (0.00 sec)

sql解释:将 s2中比s1中分数大的条目显示出来就行了(count 时需要加1)

3.取学生成绩数据,表示班级排名

mysql> select s1.*
,(select count(0) + 1 
from score s2 
where s1.class_id = s2.class_id 
and s2.score > s1.score
)rank 
from score s1 
order by class_id,rank;
+------------+----------+-------+------+
| student_id | class_id | score | rank |
+------------+----------+-------+------+
|          1 |        1 |   100 |    1 |
|          4 |        1 |    96 |    2 |
|          9 |        1 |    96 |    2 |
|          2 |        1 |    93 |    4 |
|          3 |        1 |    89 |    5 |
|          5 |        2 |    98 |    1 |
|          6 |        2 |    97 |    2 |
|          7 |        2 |    90 |    3 |
|          8 |        2 |    88 |    4 |
+------------+----------+-------+------+
9 rows in set (0.00 sec)

与之前一样,但过滤条件中只需要计算班级相同的数据条目

4.取每个班级前两名(并列的只取前面的数据)

参考文档: 
https://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值