当前位置:网站首页>Usage of exists in sql
Usage of exists in sql
2022-07-30 06:46:00 【m0_67402564】
Existing: Class Table (A_CLASS)
STUDENT
Note: The classId of the student table (STUDENT) is associated with the primary key ID of the class table (A_CLASS)
Code:
select * from STUDENT s WHERE exists (select 1 from A_ClASS c where s.CLASS_ID=c.ID)
Results
[The execution order of the exists statement is as follows]:
**1.**The outer loop (select * from student) will be executed first
**2.**Each row of the results returned by the outer loop will be taken to the inner loop for execution (at this time, note that the inner layer is also cyclically queried)
ps: The query of select * from student is (c1,c1,c2,c3); at this time, the execution order should be (
The first time: select* from A_CLASS where c1=c1, the second time: select* from A_CLASS where c1=c2,
The third time: select* from A_CLASS where c1=c1, the fourth time: select* from A_CLASS where c1=c2,
The fifth time: select* from A_CLASS where c2=c1, the sixth time: select* from A_CLASS where c2=c2,
The seventh time: select* from A_CLASS where c3=c1, the eighth time: select* from A_CLASS where c3=c2)
Note: If the inner subquery is true at this time, it will return directly and will not continue to execute this loop;
In summary: the second and fourth times will not be executed; the first and third and sixth times are eligible;
So the data whose CLASS_ID field in the STUDENT table is (c1,c2) will be queried;
If you replace the = in the above statement with != What will be the effect?
select * from STUDENT s WHERE exists (select 1 from A_ClASS c where s.CLASS_ID !=c.ID)
If the result looks like this:
Specific analysis: The execution at this time should be exactly the same as the first time, and it is also cycled 8 times:
First time: select* from A_CLASS where c1!=c1, the second time: select* from A_CLASS where c1!=c2,
Third time: select* from A_CLASS where c1!=c1, the fourth time: select* from A_CLASS where c1!=c2,
Fifth time: select* from A_CLASS where c2!=c1, sixth time: select* from A_CLASS where c2!=c2,
Seventh time: select* from A_CLASS where c3!=c1, the eighth time: select* from A_CLASS where c3!=c2
But = replaced!=; This is the sixth and eighth times not executed, the rest are all executed, s.CLASS_ID involves (c1, c2, c3) and the statements all return true;
So the data whose CLASS_ID field in the STUDENT table is (c1,c2,c3) will be queried;
Summary:
1.exists will take the value of the outer loop to query the inner layer after executing the outer loop. If the query is found, it will directly return true and terminate the current loop. If it is false, it will continue to execute until the loop is completed.If it is still false, the inner loop does not meet the conditions;
2. Do not write the inner judgment conditions!=;The result of the query will be unsatisfactory;
边栏推荐
- js基础 判断数据类型
- 强国杯初赛WP
- [HCTF 2018]admin
- sqli-labs shooting range SQL injection learning Less-1
- 【SQL】SQL 高频面试题英语版(1)
- SSTI靶场
- The operations engineer interview experience
- 信息安全必备神器之kali
- Monstache执行Monstache - f配置。toml出错不存在处理器类型和名称(附件)(= parse_exc类型
- [Mozhe Academy] Identity Authentication Failure Vulnerability Actual Combat
猜你喜欢
uni-app installs components using npm commands
npm run serve starts error npm ERR Missing script "serve"
CTF之misc-图片隐写
Monstache执行Monstache - f配置。toml出错不存在处理器类型和名称(附件)(= parse_exc类型
【墨者学院】身份认证失效漏洞实战
sqli-labs less3/4 Targeting Notes
uni-app: The use of uni-icons and how to customize icons
线程的5种状态
使用PyQt5为YoloV5添加界面(一)
sqli-labs shooting range SQL injection learning Less-1
随机推荐
node中对文件的基本操作
标准输入输出流(System.in,System.out)
【OS】操作系统高频面试题英文版(1)
复习 redux 总结
Remember a traffic analysis practice - Anheng Technology (August ctf)
DVWA installation tutorial (understand what you don't understand · in detail)
Briefly describe SSRF
sql concat()函数
[PASECA2019]honey_shop
MySQL存储引擎
【Spark】Spark 高频面试题英语版(1)
根据ip地址获取地理位置及坐标(离线方式)
SSTI range
【数仓】数据质量
[MATLAB] Image Processing - Recognition of Traffic Signs
npm install和npm install --save
TypeError The view function did not return a valid response. The function either returned None 的解决
C#中对委托的理解和使用
MySQL开窗函数
CTF之misc-内存分析(Volatility)