亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mysql實(shí)現(xiàn)查詢每門課程成績最好的前兩名學(xué)生id和姓名

 更新時(shí)間:2023年11月28日 09:36:09   作者:小蟲子啊  
這篇文章主要介紹了mysql實(shí)現(xiàn)查詢每門課程成績最好的前兩名學(xué)生id和姓名方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

創(chuàng)建庫

create database db6;

庫下面創(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 );

成績表:

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
(‘語文',1),(‘?dāng)?shù)學(xué)',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);

年級(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);

查詢每門課程成績最好的前兩名學(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ù)都是固定的

然后開始按照條件比較:

當(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ù)庫重命名語句分享

    mysql數(shù)據(jù)庫重命名語句分享

    這篇文章主要介紹了mysql數(shù)據(jù)庫重命名語句救命示例,語句中的數(shù)據(jù)庫表前綴換成自己的就可以了,大家參考使用吧
    2014-01-01
  • 關(guān)于MySQL外鍵的簡單學(xué)習(xí)教程

    關(guān)于MySQL外鍵的簡單學(xué)習(xí)教程

    這篇文章主要介紹了關(guān)于MySQL外鍵的簡單學(xué)習(xí)教程,對(duì)InnoDB引擎下的外鍵約束做了簡潔的講解,需要的朋友可以參考下
    2015-11-11
  • mysql存儲(chǔ)過程中的異常處理解析

    mysql存儲(chǔ)過程中的異常處理解析

    這篇文章主要為大家詳細(xì)介紹了mysql存儲(chǔ)過程中的異常處理,感興趣的小伙伴們可以參考一下
    2016-09-09
  • MySQL時(shí)間溢出原理、影響與解決方案

    MySQL時(shí)間溢出原理、影響與解決方案

    本文將手把手帶您了解mysql時(shí)間溢出原理、實(shí)戰(zhàn)影響與全面解決方案,所有代碼均通過dblens for mysql數(shù)據(jù)庫工具驗(yàn)證,推薦使用該工具進(jìn)行可視化數(shù)據(jù)庫管理和開發(fā),感興趣的小伙伴跟著小編一起來看看吧
    2025-03-03
  • MySQL8.0新特性之支持原子DDL語句

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

    這MySQL 8.0開始支持原子數(shù)據(jù)定義語言(DDL)語句。此功能稱為原子DDL。這篇文章主要介紹了MySQL8.0新特性——支持原子DDL語句,需要的朋友可以參考下
    2018-07-07
  • MySQL刪除表數(shù)據(jù)的方法

    MySQL刪除表數(shù)據(jù)的方法

    這篇文章主要介紹了MySQL刪除表數(shù)據(jù)的方法,小編覺得還是挺不錯(cuò)的,這里給大家分享一下,需要的朋友可以參考。
    2017-10-10
  • 在MySQL中使用mysqlbinlog flashback的簡單教程

    在MySQL中使用mysqlbinlog flashback的簡單教程

    這篇文章主要介紹了在MySQL中使用mysqlbinlog flashback的簡單教程,可以很方便地恢復(fù)數(shù)據(jù),作者還列出了使用時(shí)一些需要注意的地方,需要的朋友可以參考下
    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日志的實(shí)現(xiàn)方法

    mysql查看binlog日志的實(shí)現(xiàn)方法

    本文主要介紹了配置和查看MySQL 8.01的binlog日志,包括開啟binlog日志、配置日志格式、查看日志位置和內(nèi)容等,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-11-11
  • mysql使用source 命令亂碼問題解決方法

    mysql使用source 命令亂碼問題解決方法

    從windows上導(dǎo)出一個(gè)sql執(zhí)行文件,再倒入到unbutn中,結(jié)果出現(xiàn)亂碼,折騰7-8分鐘,解決方式在導(dǎo)出mysql sql執(zhí)行文件的時(shí)候,指定一下編碼格式
    2013-04-04

最新評(píng)論