mysql 取前200名_mysql取出某个维度下,排名前n的记录

文章目录

[隐藏]

问题描述

解题思路

问题描述

比如:有学生成绩表tb_grade:

CREATE TABLE `tb_grade` ( `studentid` int(10) unsigned DEFAULT NULL, `courseid` tinyint(4) DEFAULT NULL, `grade` int(11) DEFAULT NULL ) ENGINE=InnoDB; mysql> select * from tb_grade; +-----------+----------+-------+ | studentid | courseid | grade | +-----------+----------+-------+ | 1 | 1 | 90 | | 1 | 2 | 80 | | 1 | 3 | 85 | | 2 | 3 | 90 | | 2 | 2 | 90 | | 2 | 1 | 70 | | 3 | 1 | 95 | | 3 | 2 | 88 | | 3 | 3 | 92 | | 5 | 1 | 95 | | 5 | 2 | 90 | | 5 | 3 | 92 | +-----------+----------+-------+ 12 rows in set (0.00 sec)

现在想要查出每个courseid下,grade最高的前2个studentid。

解题思路

首先,查出每个courseid下,最高的2个grade

mysql> select courseid, grade from (select t1.courseid courseid, t1.grade grade, if(t1.grade < t2.grade, 1, 0) flag from (select distinct courseid, grade from tb_grade) t1 left join (select distinct courseid, grade from tb_grade) t2 on t1.courseid = t2.courseid) t3 group by courseid, grade having sum(flag) < 2; +----------+-------+ | courseid | grade | +----------+-------+ | 1 | 90 | | 1 | 95 | | 2 | 88 | | 2 | 90 | | 3 | 90 | | 3 | 92 | +----------+-------+ 6 rows in set (0.01 sec)

连表查询:

mysql> select t4.courseid, t4.grade, studentid from (select courseid, grade from (select t1.courseid courseid, t1.grade grade, if(t1.grade < t2.grade, 1, 0) flag from (select distinct courseid, grade from tb_grade) t1 left join (select distinct courseid, grade from tb_grade) t2 on t1.courseid = t2.courseid) t3 group by courseid, grade having sum(flag) < 2) t4 inner join tb_grade t5 on t4.courseid = t5.courseid and t4.grade = t5.grade order by courseid, grade asc; +----------+-------+-----------+ | courseid | grade | studentid | +----------+-------+-----------+ | 1 | 90 | 1 | | 1 | 95 | 3 | | 1 | 95 | 5 | | 2 | 88 | 3 | | 2 | 90 | 2 | | 2 | 90 | 5 | | 3 | 90 | 2 | | 3 | 92 | 3 | | 3 | 92 | 5 | +----------+-------+-----------+ 9 rows in set (0.00 sec)

原文出处:timd -> http://timd.cn/2017/10/09/mysql-topn/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值