五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

分享整理的12條sql語句連同數(shù)據(jù)

發(fā)布日期:2022-01-17 13:19 | 文章來源:CSDN
跟著網(wǎng)上流行的學(xué)生選課表的例子復(fù)習(xí)了一下: https://www.jb51.net/article/30655.htm 這篇文字在網(wǎng)上被轉(zhuǎn)載爛了,里面有些sql適合用在應(yīng)用系統(tǒng)里,有些“報表”的感 覺更重些,主要是想復(fù)習(xí)前者。前20條大體還挺好,后30條明顯偏報表風(fēng)格了,而 且后面選例良莠不齊,選了12個例子做練習(xí),(其實很多語法,case, any/all, union之類的都沒包括),用mysql數(shù)據(jù)庫,并共享自己造出來的數(shù)據(jù)。關(guān)于這12條 sql, 修正了原文中有紕漏的地方。 sql是基本技能,若能寫得好也挺精彩的,還在繼續(xù)練習(xí)。絕不提倡努力寫復(fù)雜sql 解決業(yè)務(wù)問題。應(yīng)用系統(tǒng)里如果存在很復(fù)雜的sql,往往揭示了業(yè)務(wù)邏輯向下泄露 到sql層的問題,不利于維護和擴展,雖然這樣確實常能提高運行效率。具體情況 自行取舍。
下面的例子都是比較通用的sql, 其實針對特定的數(shù)據(jù)庫,需要學(xué)的也挺多,比如 oracle db的decode函數(shù), rowid, rownum, connect by 雖然不通用,但是很實用。 數(shù)據(jù)可以在這里下載,只是用作練習(xí),沒做任何外鍵關(guān)聯(lián):http://xiazai.jb51.net/database/20120626051553698.txt 整理的sql在下面:
Student(S#,Sname,Sage,Ssex) 學(xué)生表
Course(C#,Cname,T#) 課程表
SC(S#,C#,score) 成績表
Teacher(T#,Tname) 教師表 1. 選出每門功課都及格的學(xué)號
select distinct `s#` from sc where `s#` not in (select `s#` from sc where score <60)
2. 查詢“1”課程比“2”課程成績高的所有學(xué)生的學(xué)號;
SELECT c01.`s#` from (select `s#`, `score` from sc where `c#`=1) c01,
(select `s#`, `score` from sc where `c#`=2) c02
where c01.`s#` = c02.`s#` and c01.score > c02.score
3. 查詢平均成績大于60分的同學(xué)的學(xué)號和平均成績;
select `s#`, avg(score) from sc group by `s#` having avg(score) > 60
4. 查詢所有同學(xué)的學(xué)號、姓名、選課數(shù)、總成績;
select student.`s#`, student.`Sname`, count(`c#`), sum(score) from student left outer join sc on student.`s#` = sc.`s#` group by student.`s#`, sc.`s#` 5.查詢沒學(xué)過“葉平”老師課的同學(xué)的學(xué)號、姓名;
select student.`s#`, student.`Sname` from student where student.`s#` not in (select distinct(sc.`s#`) from teacher, course, sc where Tname='葉平' and teacher.`t#` = course.`t#` and sc.`c#`= course.`c#` )
6. 查詢學(xué)過“001”并且也學(xué)過編號“002”課程的同學(xué)的學(xué)號、姓名
select student.`s#`, student.sname from student, sc where student.`s#` = sc.`s#` and sc.`c#` = 1 and exists (select * from sc sc_2 where sc_2.`c#`=2 and sc.`s#`=sc_2.`s#`)
7. 查詢學(xué)過“葉平”老師所教的所有課的同學(xué)的學(xué)號、姓名 (巧妙)
select `s#`, sname from student where `s#` in
(select `s#` from sc, teacher, course where tname='葉平' and teacher.`t#`=course.`t#` and course.`c#`= sc.`c#` group by `s#` having count(sc.`c#`)=
(select count(`c#`) from teacher, course where tname='葉 平' and teacher.`t#`=course.`t#`) ) 8. 查詢課程編號“002”的成績比課程編號“001”課程低的所有同學(xué)的學(xué)號、姓名 (有代表性)
select `s#`, sname from (select student.`s#`, student.sname, score, (select score from sc sc_2 where student.`s#`=sc_2.`s#` and sc_2.`c#`=2) score2 from student , sc where
sc.`s#`=student.`s#` and sc.`c#`=1) s_2 where score2 < score
9.查詢沒有學(xué)全所有課的同學(xué)的學(xué)號、姓名
select student.`S#`, Sname from student, sc where student.`s#` = sc.`s#` group by `s#`, sname having count(`c#`) < (select count(`c#`) from course) 10. 查詢至少有一門課與學(xué)號為“002”的同學(xué)所學(xué)相同的同學(xué)的學(xué)號和姓名;
select distinct(sc.`s#`), sname from student, sc where student.`s#`=sc.`s#` and `c#` in (select `c#` from sc where `s#`=002)
11. 把“SC”表中“葉平”老師教的課的成績都更改為此課程的平均成績; update sc inner join
(select sc2.`c#`, avg(sc2.score) score from sc sc2, teacher, course where
sc2.`c#`=course.`c#` and tname='葉平' and teacher.`t#` = course.`t#` and course.`c#`=sc2.`c#` group by course.`c#`) sc3 on sc.`c#`=sc3.`c#` set sc.score=sc3.score
12. 查詢2號的同學(xué)學(xué)習(xí)的課程他都學(xué)了的同學(xué)的學(xué)號;(注意理解:where語句的 第一個條件過濾掉不滿足c#的記錄,再group by,就比較清晰)
select `S#` from SC where `C#` in (select `C#` from SC where `S#`=2)
group by `S#` having count(*)=(select count(*) from SC where `S#`=2); 作者 人在江湖

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部