黑发不知勤学早,白首方悔读书迟———颜真卿
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?高級查詢
01.簡單子查詢
子查詢優(yōu)勢 ?1不用變量。2所想即所得。
?
案例1:查詢學(xué)生表中比"美洋洋"小的學(xué)員信息(姓名,地址)
?
?
案例2:查詢“oop”課程至少一次考試剛好等于60分的學(xué)生(姓名,成績)
--用子查詢?nèi)绾螌崿F(xiàn)???
--用到什么條件,交給子查詢解決
select?studentname,studentresult
from?Student,Result
where?Student.StudentNo=Result.StudentNo
and?SubjectId=(select?SubjectId?from?Subject?where?SubjectName='oop')
and?StudentResult=60
?
--只檢索學(xué)生姓名
select?studentname?
from?Student
where?StudentNo?in
(select?StudentNo?
??from?Result
???where?StudentResult=60?and?SubjectId=
????(select?SubjectId?
?????from?Subject?
?????where?SubjectName='oop'
????)
)
子查詢 小結(jié)
簡單子查詢(嵌套子查詢)的執(zhí)行機制:
將子查詢的結(jié)果作為外層父查詢的一個條件。
也就意味著先執(zhí)行子查詢,再執(zhí)行父查詢
子查詢:子查詢語句必須用小括號括起來,
然后通過比較運算符:>、<,=等連接起來
?注意點:.子查詢必須用小闊號括起來
子查詢先執(zhí)行出一個結(jié)果,然后將該結(jié)果作為父查詢
?的一個條件而存在。
in/not?in?子查詢???
案例:查詢最近一次未參加oop考試的學(xué)生名單
?
select?studentname
?
from?student
?
where?studentno?not?in
?
(
?
???select?studentno?from?result?
?
???where?subjectid?in
?
???(
?
??????select?subjectid?from?subject?where?subjectname='oop'
?
???)
?
???and?examdate=
?
???(
?
??????select?max(examdate)?from?result?where?subjectid?in
?
??????(
?
?????????select?subjectid?from?subject?where??subjectname='oop'
?
??????)
?
???)
?
)
?
and?gradeid= ?
?
??select?gradeid?from?grade?
?
??where?gradename='S1'
?
)
在這里注意 ?=??和in ? 的用法
當(dāng)返回的數(shù)據(jù)不是一行,而是多行的時候 ?使用 in ? ?反之 ?則使用= 。
?
? ? ? ? ? ? ? ? ? ?Exists和Not??Exists子查詢
?
? ? ? ? ? ? ? ? ? ? 案例:檢查“oop”課程最近一次考試。
?
select?*?from?result
?
order?by?subjectid?,examdate
?
if?exists
?
(
?
???select?*?from?result?where?subjectid=
?
???(
?
??????select?subjectid?from?subject?
?
??????where?subjectname='oop'
?
???)
?
???and?examdate=
?
???(
?
?????select?max(examdate)?from?result
?
?????where?subjectid=
?
?????(
?
??select?subjectid?from?subject?
?
??where?subjectname='oop'
?
?????)
?
???)
?
???and?studentresult>=80
?
)
?
begin
?
???update?result?set?studentresult=100
?
???where?studentresult>98
?
???and?subjectid=
?
???(
?
??????select?subjectid?from?subject?
?
??????where?subjectname='oop'
?
???)
?
???and?examdate=
?
???(
?
?????select?max(examdate)?from?result
?
?????where?subjectid=
?
?????(
?
??select?subjectid?from?subject?
?
??where?subjectname='oop'
?
?????)
?
???)
?
???update?result?set?studentresult+=2
?
???where?studentresult<=98
?
???and?subjectid=
?
???(
?
??????select?subjectid?from?subject?
?
??????where?subjectname='oop'
?
???)
?
???and?examdate=
?
???(
?
?????select?max(examdate)?from?result
?
?????where?subjectid=
?
?????(
?
??select?subjectid?from?subject?
?
??where?subjectname='oop'
?
?????)
?
???)
?
??
?
end
?
else?
?
begin
?
???update?result?set?studentresult+=5
?
???where?studentresult<=95
?
???and?subjectid=
?
???(
?
??????select?subjectid?from?subject?
?
??????where?subjectname='oop'
?
???)
?
???and?examdate=
?
???(
?
?????select?max(examdate)?from?result
?
?????where?subjectid=
?
?????(
?
??select?subjectid?from?subject?
?
??where?subjectname='oop'
?
?????)
?
???)
?
???update?result?set?studentresult=100
?
???where?studentresult>95
?
???and?subjectid=
?
???(
?
??????select?subjectid?from?subject?
?
??????where?subjectname='oop'
?
???)
?
???and?examdate=
?
???(
?
?????select?max(examdate)?from?result
?
?????where?subjectid=
?
?????(
?
??select?subjectid?from?subject?
?
??where?subjectname='oop'
?
?????)
?
???)
?
end
?
????select?studentno,studentname?from?student
?
????union
?
????select?gradeid,gradename?from?grade
?
--product??(id,proname,category)
?
select?distinct(gradename)?from?grade
?--重要:if?exists(子查詢)?子查詢返回的必須是一個結(jié)果集,而不是一個bool值。
?--結(jié)果集(用一個表結(jié)構(gòu)將數(shù)據(jù)呈現(xiàn)出來,如果沒有結(jié)果,返回的是一個空表)
?--子查詢的列可以跟單個列名,也可以跟星號,但是不能跟聚合函數(shù),因為聚合函數(shù)
?--返回的值永遠(yuǎn)是真,因為聚合函數(shù)也是結(jié)果集的一種,不能作為Exists判定的依據(jù)。
?
相關(guān)子查詢的執(zhí)行依賴于外部查詢。多數(shù)情況下是子查詢的WHERE子句中引用了外部查詢的表。執(zhí)行過程:
?
(1)從外層查詢中取出一個元組,將元組相關(guān)列的值傳給內(nèi)層查詢。
?
(2)執(zhí)行內(nèi)層查詢,得到子查詢操作的值。
?
(3)外查詢根據(jù)子查詢返回的結(jié)果或結(jié)果集得到滿足條件的行。
?
(4)然后外層查詢?nèi)〕鱿乱粋€元組重復(fù)做步驟1-3,直到外層的元組全部處理完畢。?
?
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?分頁
分頁目的:為了加快網(wǎng)站對數(shù)據(jù)的查詢(檢索)速度,我們引入了分頁的概念。
?
? ? ? ? ? 方式一:核心思想:跳過幾條取幾條(雙top?雙order?by?方式)
?
? ?--分頁查詢
? ? --雙top雙order by 例(跳五行查兩行)
? ? select top 2* from Student
? ? where StudentNo not in
? ? (select top 5 StudentNo from Student
? ? order by StudentNo)
? ? order by StudentNo
?
?
方式二:局限性(SQL?Server2005之后的版本支持該寫法,因為我們要用到row_number()?over()函數(shù),在之前是沒有該函數(shù))
?
select?*?from?
?
(select?*,row_number()?over(order?by?studentno)?as?myid?from?student)?as?temp
?
where?myid?between?4?and?6。
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
??
?
?
?
?
?
?
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhangzhenzhen/p/5261906.html
總結(jié)
以上是生活随笔為你收集整理的黑发不知勤学早,白首方悔读书迟———颜真卿的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MongoDB查询用法大全
- 下一篇: 线程的创建