黑发不知勤学早,白首方悔读书迟———颜真卿
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?高級查詢
01.簡單子查詢
子查詢優勢 ?1不用變量。2所想即所得。
?
案例1:查詢學生表中比"美洋洋"小的學員信息(姓名,地址)
?
?
案例2:查詢“oop”課程至少一次考試剛好等于60分的學生(姓名,成績)
--用子查詢如何實現???
--用到什么條件,交給子查詢解決
select?studentname,studentresult
from?Student,Result
where?Student.StudentNo=Result.StudentNo
and?SubjectId=(select?SubjectId?from?Subject?where?SubjectName='oop')
and?StudentResult=60
?
--只檢索學生姓名
select?studentname?
from?Student
where?StudentNo?in
(select?StudentNo?
??from?Result
???where?StudentResult=60?and?SubjectId=
????(select?SubjectId?
?????from?Subject?
?????where?SubjectName='oop'
????)
)
子查詢 小結
簡單子查詢(嵌套子查詢)的執行機制:
將子查詢的結果作為外層父查詢的一個條件。
也就意味著先執行子查詢,再執行父查詢
子查詢:子查詢語句必須用小括號括起來,
然后通過比較運算符:>、<,=等連接起來
?注意點:.子查詢必須用小闊號括起來
子查詢先執行出一個結果,然后將該結果作為父查詢
?的一個條件而存在。
in/not?in?子查詢???
案例:查詢最近一次未參加oop考試的學生名單
?
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 ? 的用法
當返回的數據不是一行,而是多行的時候 ?使用 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(子查詢)?子查詢返回的必須是一個結果集,而不是一個bool值。
?--結果集(用一個表結構將數據呈現出來,如果沒有結果,返回的是一個空表)
?--子查詢的列可以跟單個列名,也可以跟星號,但是不能跟聚合函數,因為聚合函數
?--返回的值永遠是真,因為聚合函數也是結果集的一種,不能作為Exists判定的依據。
?
相關子查詢的執行依賴于外部查詢。多數情況下是子查詢的WHERE子句中引用了外部查詢的表。執行過程:
?
(1)從外層查詢中取出一個元組,將元組相關列的值傳給內層查詢。
?
(2)執行內層查詢,得到子查詢操作的值。
?
(3)外查詢根據子查詢返回的結果或結果集得到滿足條件的行。
?
(4)然后外層查詢取出下一個元組重復做步驟1-3,直到外層的元組全部處理完畢。?
?
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?分頁
分頁目的:為了加快網站對數據的查詢(檢索)速度,我們引入了分頁的概念。
?
? ? ? ? ? 方式一:核心思想:跳過幾條取幾條(雙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()函數,在之前是沒有該函數)
?
select?*?from?
?
(select?*,row_number()?over(order?by?studentno)?as?myid?from?student)?as?temp
?
where?myid?between?4?and?6。
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
??
?
?
?
?
?
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/zhangzhenzhen/p/5261906.html
總結
以上是生活随笔為你收集整理的黑发不知勤学早,白首方悔读书迟———颜真卿的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MongoDB查询用法大全
- 下一篇: 线程的创建