MySQl查询各科成绩前三名
2022/1/28 19:06:21
本文主要是介绍MySQl查询各科成绩前三名,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
创建表
create table student ( name varchar(20) , lesson varchar(20), mark float ) ;
插入数据
insert into student values('john','Math',60); insert into student values('john','Eng',50); insert into student values('john','HIstory',56); insert into student values('Mike','Eng',51); insert into student values('Mike','Math',59); insert into student values('Mike','HIstory',55); insert into student values('Mark','Eng',71); insert into student values('Mark','Math',89); insert into student values('Mark','HIstory',95); insert into student values('张三','Eng',61); insert into student values('张三','Math',79); insert into student values('张三','HIstory',85); insert into student values('李明','Eng',51); insert into student values('李明','Math',69); insert into student values('李明','HIstory',95);
查询
#方法一 SELECT T1.* FROM student T1 LEFT JOIN ( SELECT DISTINCT lesson,mark FROM student) T2 ON T1.lesson = T2.lesson AND T1.mark <= T2.mark GROUP BY name,lesson,mark HAVING COUNT(1) <= 3 ORDER BY lesson,mark DESC; #方法二 SELECT s1.* FROM student s1 WHERE ( SELECT COUNT(1) FROM student s2 WHERE s1.lesson=s2.lesson AND s1.mark<s2.mark)<3 ORDER BY s1.lesson,s1.mark DESC;
这篇关于MySQl查询各科成绩前三名的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-02MySQL 3主集群搭建
- 2024-12-25如何部署MySQL集群资料:新手入门教程
- 2024-12-24MySQL集群部署资料:新手入门教程
- 2024-12-24MySQL集群资料详解:新手入门教程
- 2024-12-24MySQL集群部署入门教程
- 2024-12-24部署MySQL集群学习:新手入门教程
- 2024-12-24部署MySQL集群入门:一步一步搭建指南
- 2024-12-07MySQL读写分离入门:轻松掌握数据库读写分离技术
- 2024-12-07MySQL读写分离入门教程
- 2024-12-07MySQL分库分表入门详解