日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sql查询复习笔记-小技巧,大智慧(续一)

發(fā)布時(shí)間:2025/3/16 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sql查询复习笔记-小技巧,大智慧(续一) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
3.回答有關(guān)“至少”的問題
前言:“至多”的對(duì)立面是“至少”。通常,采用“至多”問題中表述的技巧變體,就可以解決“至少”問題。當(dāng)解決“至少”問題時(shí),把他們換成“沒有更少的”說法會(huì)更好理解。
問題(6):找到至少選擇兩門課程的學(xué)生。
Code
--?通過聚集函數(shù)count
select?sd.Tid,sd.Name,sd.Age?from?Student?sd,StudentTakeCourses?stc
where?sd.Tid=stc.Sid
group?by?sd.Tid,sd.Name,sd.Age
having?count(
*)>=2
--使用自連接查詢
select?
*?from?Student?where?Tid??in
(select?stca.sid?from?StudentTakeCourses?stca,StudentTakeCourses?stcb
where?stca.sid=stcb.sid?and?stca.cid>stcb.cid
?)
問題(7):找到同時(shí)選取sqlserver和mysql課程的學(xué)生。
Code
--?通過聚集函數(shù)count
select?sd.Tid,sd.Name,sd.Age?from?Student?sd,StudentTakeCourses?stc
where?sd.Tid=stc.Sid
and?stc.cid?in?(select?tid?from?Course?where?name='Sql?Server'?or?name='My?Sql')
group?by?sd.Tid,sd.Name,sd.Age
having?count(stc.cid)?>=2

--?通過聚集函數(shù)min和max
select?sd.Tid,sd.Name,sd.Age?from?Student?sd,StudentTakeCourses?stc
where?sd.Tid=stc.Sid
and?stc.cid?in?(select?tid?from?Course?where?name='Sql?Server'?or?name='My?Sql')
group?by?sd.Tid,sd.Name,sd.Age
having?min(stc.cid)?<>max(stc.cid)

--使用自連接查詢
select?sd.*?from?Student?sd,StudentTakeCourses?stca,StudentTakeCourses?stcb
where?sd.Tid=stca.sid?and?stca.sid=stcb.sid
and?stca.cid?=(select?tid?from?Course?where?name='Sql?Server')
and?stcb.cid=(select?tid?from?Course?where??name='My?Sql')
問題(8):找到至少比兩個(gè)學(xué)生大的學(xué)生。
Code

--?通過聚集函數(shù)count
select?sd.*?from?Student?sd
where?2<=(select?count(*)?from?Student?sd1?where?sd1.age<sd.age)


--使用自連接查詢
select?distinct?sd.*?from?Student?sd,Student?sd1,Student?sd2
where?sd.age>sd1.age?and?sd1.age>sd2.age
4.回答有關(guān)“準(zhǔn)確”的問題
問題(9):找到只教一門課程的教授。
Code
--?通過聚集函數(shù)count
select?pf.*?from?Professor?pf,ProfessorTeachCourses?ptc
where?pf.Tid=ptc.pid?
group?by?pf.Tid,pf.Name,pf.Dept,pf.Salary,pf.Age
having?count(*)=1

--使用自連接查詢
select?pf.*?from?Professor?pf,ProfessorTeachCourses?ptc
where?pf.Tid=ptc.pid?
and?pf.Tid?not?in
(?
select?ptc1.Pid?from?ProfessorTeachCourses?ptc1,ProfessorTeachCourses?ptc2
where?ptc1.Pid=ptc2.Pid?and?ptc1.Cid?>ptc2.Cid
)
問題(10):找到只選擇sqlserver和mysql的學(xué)生(只選取了這兩門課程,沒有選擇其他課程)
Code
--使用自連接查詢
select?sd.*?from?Student?sd,StudentTakeCourses?stc1,StudentTakeCourses?stc2
where?sd.Tid=stc1.Sid?and?stc1.Sid?=stc2.Sid?
and?stc1.Cid=(select?Tid?from?Course?where?Name='Sql?Server')
and?stc2.Cid=(select?Tid?from?Course?where?Name='My?Sql')
--排除選課超過三門的學(xué)生
and?sd.Tid?not?in(
select?sd1.Tid?from?Student?sd1,StudentTakeCourses?stca,StudentTakeCourses?stcb,StudentTakeCourses?stcd
where?sd1.Tid=stca.Sid
and?sd1.Tid=stcb.Sid
and?stca.Cid>stcb.Cid
and?stcb.Cid>stcd.Cid
)
問題(11):找到只比兩個(gè)學(xué)生大的學(xué)生(也就是找到第三年輕的學(xué)生)
Code
--?通過聚集函數(shù)count
select?sd.*?from?Student?sd
where?2=(select?count(*)?from?Student?sd1
where?sd1.Age<sd.Age
)

--使用自連接查詢
select?sd1.*?from?Student?sd1,Student?sd2,Student?sd3
where?sd1.age>sd2.age?and?sd2.age>sd3.age
and?sd1.Tid?not?in
--排除比三個(gè)學(xué)生以上大的學(xué)生
(select?sd4.Tid?from?Student?sd4,Student?sd5,Student?sd6,Student?sd7
where?sd4.age>sd5.age?and?sd5.age>sd6.age?and?sd6.age>sd7.age
)

5:回答有關(guān)“一些”或“所有”的問題
問題(12):找到選取所有課程的學(xué)生。

select?sd.Tid,sd.Name,sd.Age?from?Student?sd,StudentTakeCourses?stc
where?sd.Tid=stc.Sid
group?by?sd.Tid,sd.Name,sd.Age?
having?count(stc.Cid)=(select?count(*)?from?course)?--根據(jù)課程數(shù)排除課程沒有都選的學(xué)生

問題(13):找到比其他所有學(xué)生都大的學(xué)生。(找到年齡最大的學(xué)生)

--使用max聚集函數(shù)
select?*?from?Student?where?Age=(select?max(Age)?from?Student)

還有一種比較常見的方式:

--常見方式
select?*?from?Student?where?Age>=all?(select?Age?from?Student)

最后通過自連接也可以查詢到結(jié)果,不過沒有上面兩種看起來直接了當(dāng):

--自連接方式
select?*?from?Student?
where?Age?not?in?
(
select?sd1.Age?from?Student?sd1,student?sd2
where?sd1.Age<sd2.Age
)

<完>

總結(jié)

以上是生活随笔為你收集整理的sql查询复习笔记-小技巧,大智慧(续一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。