網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
現(xiàn)有:班級(jí)表(A_CLASS)
學(xué)生表( STUDENT)
注:學(xué)生表(STUDENT)的classId關(guān)聯(lián)班級(jí)表(A_CLASS)的主鍵ID
代碼:
select * from STUDENT s WHERE exists (select 1 from A_ClASS c where s.CLASS_ID=c.ID)
結(jié)果
【exists語(yǔ)句的執(zhí)行順序如下】:
1.首先會(huì)執(zhí)行外循環(huán)(select * from student
)
2.外循環(huán)返回的結(jié)果每一行都會(huì)拿著去內(nèi)層循環(huán)執(zhí)行(此時(shí)注意,內(nèi)層也是循環(huán)查詢的)
ps:select * from student
查詢的為 (c1,c1,c2,c3);此時(shí)執(zhí)行順序應(yīng)該是(
第一次:select* from A_CLASS where c1=c1
,第二次:select* from A_CLASS where c1=c2
,
第三次:select* from A_CLASS where c1=c1
,第四次:select* from A_CLASS where c1=c2
,
第五次:select* from A_CLASS where c2=c1
,第六次:select* from A_CLASS where c2=c2
,
第七次:select* from A_CLASS where c3=c1
,第八次:select* from A_CLASS where c3=c2)
注意:此時(shí)的內(nèi)層子查詢?nèi)绻麨閠rue,則直接返回不會(huì)再繼續(xù)執(zhí)行本次循環(huán);
綜上所述:第二次和第四次是不會(huì)被執(zhí)行的;? 第一次和第三次還有第六次是符合條件的;
所以?STUDENT表中CLASS_ID字段為(c1,c2)的數(shù)據(jù)都會(huì)被查詢出來;
如果將 上述語(yǔ)句的=換成!=會(huì)是什么效果?
select * from STUDENT s WHERE exists (select 1 from A_ClASS c where s.CLASS_ID!=c.ID)
結(jié)果如果所示:?
具體分析一下:此時(shí)的執(zhí)行應(yīng)該和第一次一模一樣也是循環(huán)8次分別為:
第一次:select* from A_CLASS where c1!=c1
,第二次:select* from A_CLASS where c1!=c2
,
第三次:select* from A_CLASS where c1!=c1
,第四次:select* from A_CLASS where c1!=c2
,
第五次:select* from A_CLASS where c2!=c1
,第六次:select* from A_CLASS where c2!=c2
,
第七次:select* from A_CLASS where c3!=c1
,第八次:select* from A_CLASS where c3!=c2
但是=換成了!=;這次是第六次和第八次沒有被執(zhí)行,其余的全被執(zhí)行,s.CLASS_ID涉及到(c1,c2,c3)并且語(yǔ)句都返回了true;
所以STUDENT表中CLASS_ID字段為(c1,c2,c3)的數(shù)據(jù)都會(huì)被查詢出來;
附:exists與in比較
in的用法相信大家很好理解。
select * from table_name where col_name [not] in(子查詢);
- 先運(yùn)行子查詢,生成結(jié)果集
- 再運(yùn)行外查詢時(shí),判斷col_name在不在子查詢里,在的話則返回該行,不在則不返回。
- col_name的列數(shù)和子查詢的列數(shù)要相對(duì)應(yīng)
當(dāng)外大子小時(shí),即查詢的內(nèi)容很大時(shí),判斷次數(shù)少,in優(yōu)于exist【子查詢小用in】
當(dāng)外小子大時(shí),即查詢內(nèi)容很小時(shí),代入次數(shù)少,exists優(yōu)于in【子查詢大用exists】
總結(jié):
1.exists執(zhí)行外循環(huán)后,會(huì)拿著外循環(huán)的值,去內(nèi)層查詢,如果查詢到就直接返回true,并且終止本次循環(huán),如果是false,則會(huì)一直執(zhí)行,直至循環(huán)完成還為false,則本次內(nèi)循環(huán)不符合條件;
2.內(nèi)層的判斷條件不要寫!=;查詢的結(jié)果會(huì)不盡人意;
原文鏈接:https://blog.csdn.net/ibhjvbg/article/details/123344265
相關(guān)推薦
- 2022-05-13 c++ remove_reference
- 2022-04-27 詳解linux里的backlog參數(shù)_Linux
- 2022-03-15 3 Segmentation fault (core dumped) ./a.out Exited
- 2022-08-20 Python?實(shí)現(xiàn)一個(gè)全連接的神經(jīng)網(wǎng)絡(luò)_python
- 2023-04-12 Pandas創(chuàng)建DataFrame提示:type?object?'object'?has?no?at
- 2022-10-13 云服務(wù)器Windows?Server2012配置FTP服務(wù)器詳細(xì)圖文教程_FTP服務(wù)器
- 2022-07-21 vscode代碼保存時(shí)自動(dòng)格式化
- 2022-10-02 Go語(yǔ)言快速入門指針Map使用示例教程_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支