第四模塊MySQL50題作業,以及由作業引申出來的一些高端玩法

来源:https://www.cnblogs.com/oceanicstar/archive/2018/06/09/9160177.html
-Advertisement-
Play Games

一、表關係 先參照如下表結構創建7張表格,並創建相關約束 年級表: class_grade 1. 班級表class create table class ( cid int primary key auto_increment, caption char(10), grade_id int ); i ...


一、表關係

先參照如下表結構創建7張表格,並創建相關約束

        
班級表:class       學生表:student      
cid caption grade_id   sid sname gender class_id
1 一年一班 1   1 喬丹 1
2 二年一班 2   2 艾弗森 1
3 三年二班 3   3 科比 2
               
老師表:teacher       課程表:course      
tid tname     cid cname teacher_id  
1 張三     1 生物 1  
2 李四     2 體育 1  
3 王五     3 物理 2  
               
成績表:score        

年級表:

class_grade

   
sid student_id course_id score   gid gname  
1 1 1 60   1 一年級  
2 1 2 59   2 二年級  
3 2 2 99   3 三年級  
               
班級任職表:teach2cls              
tcid tid cid          
1 1 1          
2 1 2          
3 2 1          
4 3 2        

 

1. 班級表class

create table class
    (
    cid int primary key auto_increment,
    caption char(10),
    grade_id int
    );
【創建表語句】
insert into class values
(1,'少一一班',1),
(2,'少二一班',2),
(3,'少三二班',3),
(4,'少四一班',4),
(5,'少五三班',5);
【插入記錄語句】

2. 學生表student

create table student
    (
    sid int primary key auto_increment,
    sname char(10),
    gender enum('','') not null,
    class_id int
    );
【創建表語句】
insert into student values
(1,'喬丹','',1),
(2,'艾弗森','',1),
(3,'科比','',2),
(4,'葫蘆娃','',3),
(5,'張三豐','',5),
(6,'洞房不敗','',4),
(7,'櫻木花道','',2),
(8,'松島菜菜子','',3),
(9,'洞房不敗','',5);
【插入記錄語句】

3. 老師表teacher

create table teacher
    (
    tid int primary key auto_increment,
    tname char(10)
    );
【創建表語句】
insert into teacher values
(1,'張三'),
(2,'李四'),
(3,'王五'),
(4,'蕭峰'),
(5,'一休哥'),
(6,'諸葛'),
(7,'李四');
【插入記錄語句】

4. 課程表course

create table course
(
cid int primary key auto_increment,
cname char(10),
teacher_id int
);
【創建表語句】
insert into course values
(1,'生物',1),
(2,'體育',1),
(3,'物理',2),
(4,'數學',3),
(5,'語文',4),
(6,'英語',2),
(7,'土遁?沙地送葬',5),
(8,'夏日喂蚊子大法',3),
(9,'麻將牌九撲克千術',6);
【插入記錄語句】

5. 成績表score

create table score
(
sid int primary key auto_increment,
student_id int,
course_id int,
score int
);
【創建表語句】
insert score values
(1,1,1,60),
(2,1,2,21),
(3,2,2,99),
(4,3,3,56),
(5,4,1,56),
(6,5,3,94),
(7,5,4,40),
(8,6,4,80),
(9,7,3,37),
(10,8,5,100),
(11,8,6,89),
(12,8,7,0),
(13,3,8,45),
(14,7,1,89),
(15,2,7,89),
(16,2,1,61);
【插入記錄語句】

6. 年級表class_grade

create table class_grade
    (
    gid int primary key auto_increment,
    gname char(10)
    );
【創建表語句】
insert class_grade values
(1,'少一年級'),
(2,'少二年級'),
(3,'少三年級'),
(4,'少四年級'),
(5,'少五年級');
【插入記錄語句】

7. 班級任職表teach2cls

create table teach2cls
    (
    tcid int primary key auto_increment,
    tid int,
    cid int
    );
【創建表語句】
insert into teach2cls values
(1,1,1),
(2,1,2),
(3,2,1),
(4,3,2),
(5,4,5),
(6,5,3),
(7,5,5),
(8,6,2),
(9,6,4),
(10,6,3),
(11,4,1),
(12,1,4);
【插入記錄語句】

 

二、操作表

★註:由於樣本數量有限,為了能夠得到足夠的查詢結果,所有題目中涉及到“超過”或“以上”字樣的,均預設為包含該值

(例如:查詢教授課程超過2門的老師的id和姓名,視作教授課程數>=2)

 

1、自行創建測試數據;

(創建語句見"一、表關係")


2、查詢學生總人數

select 
    count(*) as 學生總人數 
from 
    student;

 

3、查詢“生物”課程和“物理”課程成績都及格的學生id和姓名

【查法1——子查詢】

select 
    sid, sname
from 
    student 
where 
    sid in
    (
    select 
        student_id
    from 
        score
    where 
        student_id in
            (select 
                student_id
            from 
                score
            where 
                course_id = (select cid from course where cname = '生物') 
                and 
                score >= 60)
        and 
            course_id = 
            (select cid from course where cname = '物理') 
        and 
            score >= 60
    );

【查法2——聯表】

select 
    sid, 
    sname 
from 
    student
where 
    sid in 
    (
    select 
        t1.student_id 
    from 
        (
        select 
            student_id 
        from 
            score
        where 
            course_id = (select cid from course where cname = '生物') 
            and 
            score >= 60
        ) as t1
        inner join 
            (
            select 
                student_id 
            from 
                score 
            where 
                course_id = (select cid from course where cname = '物理') 
                and 
                score >= 60
            ) as t2
        on 
            t1.student_id=t2.student_id
    );

 

4、查詢每個年級的班級數,取出班級數最多的前三個年級

select 
    class.grade_id, 
    class_grade.gname, 
    count(class.cid) as 班級數
from 
    class inner join class_grade 
    on class.grade_id=class_grade.gid
group by 
    class.grade_id
order by 
    count(class.cid) desc
limit 3;

 

5、查詢平均成績最高和最低的學生的id和姓名以及平均成績

select 
    stu.sid, 
    stu.sname, 
    avg(score) as 平均成績
from 
    student as stu inner join score as sco
    on stu.sid = sco.student_id
group by 
    stu.sid
having 
    avg(score) = 
        (
        select 
            avg(score) 
        from 
            score 
        group by 
            student_id
        order by 
            avg(score) desc
        limit 1
        ) 
    or 
        avg(score) = 
            (
            select 
                avg(score) 
            from 
                score 
            group by 
                student_id
            order by 
                avg(score) asc
            limit 1
            );

 

6、查詢每個年級的學生人數

select 
    t1.gname, 
    count(s.sid) as 學生人數
from 
    (
    select 
        * 
    from 
        class as c inner join class_grade as g 
        on c.grade_id = g.gid
    ) as t1
    inner join 
        student as s 
    on 
        t1.cid = s.class_id
group by 
    t1.gid;

 

7、查詢每位學生的學號,姓名,選課數,平均成績

 

select 
    stu.sid as 學號,
    stu.sname as 姓名,
    count(sco.course_id) as 選課數,
    avg(sco.score) as 平均成績
from 
    student as stu left join score as sco 
    on stu.sid = sco.student_id
group by 
    sco.student_id;

 

8、查詢學生編號為“2”的學生的姓名、該學生成績最高的課程名、成績最低的課程名及分數

select 
    t1.sname as 姓名,
    t2.cname as 課程名,
    t1.score as 分數
from 
    (select 
        stu.sid, stu.sname, sco.course_id, sco.score 
    from 
        student as stu inner join score as sco 
        on stu.sid = sco.student_id 
        where stu.sid=2) as t1
    inner join
        course as t2 
    on 
        t1.course_id = t2.cid
group by 
    t2.cid
having 
    score in (max(score),min(score));

 

9、查詢姓“李”的老師的個數和所帶班級數;

select 
    count(te.tid) as 姓李老師個數,
    count(tc.cid) as 所帶班級數
from 
    teacher as te inner join teach2cls as tc
    on te.tid = tc.tid
where 
    te.tname regexp "^李.*"
group by 
    te.tid;

 

10、查詢班級數小於5的年級id和年級名;

select 
    c.grade_id as 年級id,
    g.gname as 年級名
from 
    class as c inner join class_grade as g
    on c.grade_id = g.gid
group by 
    c.grade_id
having 
    count(c.cid)<5;

 

11、查詢班級信息,包括班級id、班級名稱、年級、年級級別(12為低年級,34為中年級,56為高年級),示例結果如下;

select 
    cid as 班級id,
    caption as 班級名稱,
    gname as 年級,
    case
        when g.gid in (1,2) then '低年級'
        when g.gid in (3,4) then '中年級'
        when g.gid in (5,6) then '高年級'
        else '其他' 
    end as 年級級別
from 
    class as c inner join class_grade as g
    on c.grade_id = g.gid;

 

12、查詢學過“張三”老師2門課以上的同學的學號、姓名;

select 
    stu.sid as 學號,
    stu.sname as 姓名
from 
    student as stu inner join score as sco 
    on stu.sid = sco.student_id
where 
    sco.course_id in 
        (
        select 
            c.cid
        from 
            teacher as t inner join course as c
            on t.tid = c.teacher_id
        where 
            t.tname = '張三'
        )
group by 
    stu.sid
having 
    count(sco.course_id) >= 2;


13、查詢教授課程超過2門的老師的id和姓名;

select
    tid as id,
    tname as 姓名
from 
    teacher as t inner join course as c 
    on t.tid = c.teacher_id
group by 
    c.teacher_id
having 
    count(c.cid) >= 2;

 

14、查詢學過編號“1”課程和編號“2”課程的同學的學號、姓名;

select 
    sid as 學號,
    sname as 姓名
from 
    student
where 
    sid in 
        (
        select 
            student_id 
        from 
            score
        where 
            student_id in 
                (
                select 
                    student_id 
                from 
                    score
                where 
                    course_id = 1
                )
            and 
                course_id = 2
        );

 

15、查詢沒有帶過高年級的老師id和姓名;

select 
    tid as 老師id,
    tname as 姓名
from 
    teacher
where 
    tid not in 
        (
        select 
            tc.tid
        from 
            class as c inner join teach2cls as tc 
            on c.cid = tc.cid
        where 
            c.grade_id in (5,6)
        );

 

16、查詢學過“張三”老師所教的所有課的同學的學號、姓名;

select 
    distinct
    stu.sid as 學號,
    stu.sname as 姓名
from 
    student as stu inner join score as sco 
    on stu.sid = sco.student_id
where 
    sco.course_id in 
        (
        select 
            c.cid 
        from 
            teacher as t inner join course as c 
            on t.tid = c.teacher_id
        where 
            t.tname = "張三"
        );

 

17、查詢帶過超過2個班級的老師的id和姓名;

select 
    tid as id,
    tname as 姓名
from 
    teacher
where 
    tid in 
        (
        select 
            tid 
        from 
            teach2cls
        group by 
            tid
        having 
            count(cid) >= 2
        );

 

18、查詢課程編號“2”的成績比課程編號“1”課程低的所有同學的學號、姓名;

select 
    sid as 學號,
    sname as 姓名
from 
    student
where 
    sid in
        (
        select 
            t1.student_id
        from 
            (
            select 
                * 
            from 
                score
            where 
                course_id = 1
            ) as t1
            inner 
              
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、安裝 安裝之前先將伺服器的防火牆關掉。 systemctl stop firewalld systemctl disable firewall 第一步: 安裝apr 下載: wget -c http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1 ...
  • 1. 選擇鏡像 2. 安裝CentOS7 3. 其他必要修改 註意:安裝好後,沒有ifconfig命令 3.1 主機名修改 3.2 網卡名修改 配置修改 我們在命令行中鍵入#:vim /etc/default/grub命令來編輯環境變數值。 併在變數GRUB_CMDLINE_LINU中加入:net. ...
  • 1. 搭建虛擬化環境常見故障講解 2. 安裝CentOS Linux系統 ……………… PS:運維老鳥教你安裝centos6.5如何選擇安裝包 3. 遠程連接LInux ip配置 註意:不用做任何修改 步驟: 參數講解: 4. 網卡最終設置 5. 安裝完之後系統基礎優化 6. 參考文章 1、運維老鳥 ...
  • 檢查硬體要求 系統必須滿足下麵最小的硬體要求 記憶體要求 Minimum: 1 GB of RAMRecommended: 2 GB of RAM or more To determine the RAM size, enter the following command: The following ...
  • 一、準備工作 1、IDE的pom.xml中添加 2、IDE的reources中放入centos中Hbase的三個配置文件 core-site.xmlhbase-site.xmlhdfs-site.xml 註:文件路徑:/soft/hbase/conf/**** 二、Hbase -- API操作 組成 ...
  • 單表查詢 1、查詢所有: select * from 表名; 2、查詢選中欄位數據: select 欄位名 from 表名; 3、查詢指定條件下的數據: select 欄位名 from 表名 where 條件(例id>3); 4、查詢後為欄位取別名 as: select 原名 as 更改名 from ...
  • 背景 由於項目的需要,使用 資料庫,因此在Windows上安裝 資料庫。但是在安裝後,無法訪問本地資料庫,這個時候查看 目錄,沒有任何文件。而且安裝過程中,彈出提示框 Problem running post install step.Installation may not complete co ...
  • 在鎖與事務系列里已經寫完了上篇中篇,這次寫完下篇。這個系列俺自認為是有條不紊的進行,但感覺鎖與事務還是有多很細節沒有講到,溫故而知新可以為師矣,也算是一次自我提高總結吧,也謝謝大伙的支持。在上一篇的末尾寫了事務隔離級別的不同表現,還沒寫完,只寫到了重覆讀的不同隔離表現,這篇繼續寫完序列化,快照的不同 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...