mysql實(shí)現(xiàn)查詢每門(mén)課程成績(jī)最好的前兩名學(xué)生id和姓名
創(chuàng)建庫(kù)
create database db6;
庫(kù)下面創(chuàng)建表
班級(jí)表:
mysql> create table class(cid int not null unique auto_increment, -> caption varchar(6) not null, -> grade_id int not null);
學(xué)生表:
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 );
成績(jī)表:
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);
年級(jí)表:
mysql> create table class_grade(gid int not null unique auto_increment, -> gname varchar(6) not null );
班級(jí)任職表
mysql> create table teach2cls(tcid int not null unique auto_increment, -> tid int not null, -> cid int not null);
自建數(shù)據(jù)
班級(jí)表:
insert class (caption,grade_id) values (‘一年一班',1),(‘一年二班',1), (‘二年一班',2), (‘三年一班',3),(‘三年二班',3),(‘三年三班',3), (‘四年一班',4),(‘四年二班',4),(‘四年三班',4),(‘四年四班',4);
學(xué)生表:
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 (‘語(yǔ)文',1),(‘?dāng)?shù)學(xué)',2),(‘英語(yǔ)',1),(‘物理',3);
成績(jī)表:
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);
年級(jí)表:
insert class_grade(gname) values (‘一年級(jí)'),(‘二年級(jí)'),(‘三年級(jí)'),(‘四年級(jí)');
班級(jí)任職表:
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);
查詢每門(mén)課程成績(jī)最好的前兩名學(xué)生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(*)都是統(tǒng)計(jì)記錄的行數(shù)
然后,這個(gè)子查詢的作用是記錄當(dāng)前查詢行和子查詢所有符合條件的數(shù)量
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 結(jié)果0
- 然后匹配第二條記錄 2----------5---------90
- 然后子查詢 select count(1) from score as sc where 5=sc.course_id and 90< sc.score 結(jié)果為1
- 然后匹配第三條記錄 3----------6----------80
- 然后子查詢 select count(1) from score as sc where 6 = sc.course_id and 80 < sc.score 結(jié)果為2
那么:
又因后面比較了count的結(jié)果要<2,所以只有兩條記錄符合,
上面的原理大概這樣:
首先有兩個(gè)列表 s1 和 s2 ,
s1 是 select * from score as s1 獲得 , s2 是子查詢中 select count(1) from score as s2 獲得
此時(shí)s1的學(xué)生id、課程id、分?jǐn)?shù)都是固定的
然后開(kāi)始按照條件比較:
當(dāng)s1 的課程id 為 4 的時(shí)候, s1.course_id = s2.course_id and s1.score < s2.score
- 這里的s2.course_id 包含了表里所有學(xué)生只要課程id是4的所有記錄
- 然后就拿著s1 的分?jǐn)?shù) 和 s2 所有學(xué)生的分?jǐn)?shù)進(jìn)行比較
- 當(dāng)s2 的學(xué)生分?jǐn)?shù)大于s1 的這個(gè)學(xué)生分?jǐn)?shù)的時(shí)候 count(1) 統(tǒng)計(jì)就會(huì)+1
- 然后count<2,就只拿出兩條記錄
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mysql數(shù)據(jù)庫(kù)重命名語(yǔ)句分享
這篇文章主要介紹了mysql數(shù)據(jù)庫(kù)重命名語(yǔ)句救命示例,語(yǔ)句中的數(shù)據(jù)庫(kù)表前綴換成自己的就可以了,大家參考使用吧2014-01-01關(guān)于MySQL外鍵的簡(jiǎn)單學(xué)習(xí)教程
這篇文章主要介紹了關(guān)于MySQL外鍵的簡(jiǎn)單學(xué)習(xí)教程,對(duì)InnoDB引擎下的外鍵約束做了簡(jiǎn)潔的講解,需要的朋友可以參考下2015-11-11在MySQL中使用mysqlbinlog flashback的簡(jiǎn)單教程
這篇文章主要介紹了在MySQL中使用mysqlbinlog flashback的簡(jiǎn)單教程,可以很方便地恢復(fù)數(shù)據(jù),作者還列出了使用時(shí)一些需要注意的地方,需要的朋友可以參考下2015-05-05The 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 statement2020-08-08mysql查看binlog日志的實(shí)現(xiàn)方法
本文主要介紹了配置和查看MySQL 8.01的binlog日志,包括開(kāi)啟binlog日志、配置日志格式、查看日志位置和內(nèi)容等,具有一定的參考價(jià)值,感興趣的可以了解一下2024-11-11