mysql实现查询每门课程成绩最好的前两名学生id和姓名

 更新时间:2023年11月28日 09:36:09   作者:小虫子啊  
这篇文章主要介绍了mysql实现查询每门课程成绩最好的前两名学生id和姓名方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

创建库

create database db6;

库下面创建表

班级表:

mysql> create table class(cid int not null unique auto_increment,
-> caption varchar(6) not null,
-> grade_id int not null);

学生表:

mysql> create table student(sid int not null unique auto_increment,
-> sname varchar(3) not null,
-> gender enum(‘male',‘female') not null default ‘male',
-> class_id int not null);

老师表:

mysql> create table teacher(tid int not null unique auto_increment,
-> tname varchar(6));

课程表:

mysql> create table course(cid int not null unique auto_increment,
-> cname varchar(4) not null,
-> teacher_id int );

成绩表:

mysql> create table score(sid int not null unique auto_increment,
-> student_id int not null,
-> course_id int not null,
-> score int not null);

年级表:

mysql> create table class_grade(gid int not null unique auto_increment,
-> gname varchar(6) not null );

班级任职表

mysql> create table teach2cls(tcid int not null unique auto_increment,
-> tid int not null,
-> cid int not null);

自建数据

班级表:

insert class (caption,grade_id) values
(‘一年一班',1),(‘一年二班',1),
(‘二年一班',2),
(‘三年一班',3),(‘三年二班',3),(‘三年三班',3),
(‘四年一班',4),(‘四年二班',4),(‘四年三班',4),(‘四年四班',4);

学生表:

insert student(sname,gender,class_id) values
(‘后羿',‘male',1),(‘盖伦',‘male',2),(‘潘金莲',‘female',1),(‘诸葛亮',‘male',3),
(‘曹操',‘male',3),(‘嬴政',‘male',4),(‘嫦娥',‘female',7),(‘黑老大',‘female',6),
(‘王老二',‘male',5);

老师表:

insert teacher(tname) values (‘狗蛋'),(‘二锅'),(‘老肥');

课程表:

insert course(cname,teacher_id) values
(‘语文',1),(‘数学',2),(‘英语',1),(‘物理',3);

成绩表:

insert score(student_id,course_id,score) values
(1,1,61),(1,2,59),(1,3,10),
(2,4,80),(2,3,25),
(3,1,90),(3,2,33),(3,3,100),(3,4,16),
(4,1,100),(4,2,100),(4,4,100),
(5,1,95),(5,2,19),(5,3,59),
(6,3,11),(6,2,61),
(7,3,85),(7,4,99),
(8,1,85),
(9,2,100);

年级表:

insert class_grade(gname) values
(‘一年级'),(‘二年级'),(‘三年级'),(‘四年级');

班级任职表:

insert teach2cls (tid,cid) values
(1,1),(1,3),(2,2),(3,4),(2,8),(3,6),(1,7),(3,10),(3,9),(1,5);

查询每门课程成绩最好的前两名学生id和姓名

select * from score
where (select count(1)
from score as sc
where score.course_id = sc.course_id
and score.score < sc.score)<2
order by course_id,score desc;
#首先count(1)和count(*)都是统计记录的行数

然后,这个子查询的作用是记录当前查询行和子查询所有符合条件的数量

select count(1) from score as sc where score.course_id = sc.course_id and score.score < sc.score

比如:

id-------course-------score
1----------4 ---------100
2 ---------5 ------------90
3 --------6 ------------80

(只是举例子所列的表)

那么查询的效果就是 select * from score 按行查询

先匹配第一条记录 1-------------4---------------100

  • 然后子查询 select count(1) from score as sc where 4 = sc.course_id and 100 <sc.score 结果0
  • 然后匹配第二条记录 2----------5---------90
  • 然后子查询 select count(1) from score as sc where 5=sc.course_id and 90< sc.score 结果为1
  • 然后匹配第三条记录 3----------6----------80
  • 然后子查询 select count(1) from score as sc where 6 = sc.course_id and 80 < sc.score 结果为2

那么:

又因后面比较了count的结果要<2,所以只有两条记录符合,

上面的原理大概这样:

首先有两个列表 s1 和 s2 ,

s1 是 select * from score as s1 获得 , s2 是子查询中 select count(1) from score as s2 获得

此时s1的学生id、课程id、分数都是固定的

然后开始按照条件比较:

当s1 的课程id 为 4 的时候, s1.course_id = s2.course_id and s1.score < s2.score

  • 这里的s2.course_id 包含了表里所有学生只要课程id是4的所有记录
  • 然后就拿着s1 的分数 和 s2 所有学生的分数进行比较
  • 当s2 的学生分数大于s1 的这个学生分数的时候 count(1) 统计就会+1
  • 然后count<2,就只拿出两条记录

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • mysql数据库重命名语句分享

    mysql数据库重命名语句分享

    这篇文章主要介绍了mysql数据库重命名语句救命示例,语句中的数据库表前缀换成自己的就可以了,大家参考使用吧
    2014-01-01
  • 关于MySQL外键的简单学习教程

    关于MySQL外键的简单学习教程

    这篇文章主要介绍了关于MySQL外键的简单学习教程,对InnoDB引擎下的外键约束做了简洁的讲解,需要的朋友可以参考下
    2015-11-11
  • mysql存储过程中的异常处理解析

    mysql存储过程中的异常处理解析

    这篇文章主要为大家详细介绍了mysql存储过程中的异常处理,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • MySQL时间溢出原理、影响与解决方案

    MySQL时间溢出原理、影响与解决方案

    本文将手把手带您了解mysql时间溢出原理、实战影响与全面解决方案,所有代码均通过dblens for mysql数据库工具验证,推荐使用该工具进行可视化数据库管理和开发,感兴趣的小伙伴跟着小编一起来看看吧
    2025-03-03
  • MySQL8.0新特性之支持原子DDL语句

    MySQL8.0新特性之支持原子DDL语句

    这MySQL 8.0开始支持原子数据定义语言(DDL)语句。此功能称为原子DDL。这篇文章主要介绍了MySQL8.0新特性——支持原子DDL语句,需要的朋友可以参考下
    2018-07-07
  • MySQL删除表数据的方法

    MySQL删除表数据的方法

    这篇文章主要介绍了MySQL删除表数据的方法,小编觉得还是挺不错的,这里给大家分享一下,需要的朋友可以参考。
    2017-10-10
  • 在MySQL中使用mysqlbinlog flashback的简单教程

    在MySQL中使用mysqlbinlog flashback的简单教程

    这篇文章主要介绍了在MySQL中使用mysqlbinlog flashback的简单教程,可以很方便地恢复数据,作者还列出了使用时一些需要注意的地方,需要的朋友可以参考下
    2015-05-05
  • The MySQL server is running with the --read-only option so it cannot execute this statement

    The MySQL server is running with the --read-only option so i

    1209 - The MySQL server is running with the --read-only option so it cannot execute this statement
    2020-08-08
  • mysql查看binlog日志的实现方法

    mysql查看binlog日志的实现方法

    本文主要介绍了配置和查看MySQL 8.01的binlog日志,包括开启binlog日志、配置日志格式、查看日志位置和内容等,具有一定的参考价值,感兴趣的可以了解一下
    2024-11-11
  • mysql使用source 命令乱码问题解决方法

    mysql使用source 命令乱码问题解决方法

    从windows上导出一个sql执行文件,再倒入到unbutn中,结果出现乱码,折腾7-8分钟,解决方式在导出mysql sql执行文件的时候,指定一下编码格式
    2013-04-04

最新评论