当前位置:网站首页>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;
边栏推荐
猜你喜欢

PHP-fpm

Flink CDC 实现Postgres到MySQL流式加工传输案例

php-fpm

C#下大批量一键空投实现

Blind injection, error injection, wide byte injection, stack injection study notes
CTF之misc-其他类型隐写

TDengine集群搭建

Solution to TypeError The view function did not return a valid response. The function either returned None

Dcat Admin 安装

Dcat Admin installation
随机推荐
[Mini Program Project Development--Jingdong Mall] Classification Navigation Area of uni-app
sqli-labs靶场 SQL注入学习 Less-1
冒泡排序、选择排序、插入排序、快速排序
史上超强最常用SQL语句大全
MySQL-Explain详解
c#下Web3合约空投、转账调用代码
文件上传漏洞的绕过
【面经】米哈游数据开发面经
SQL Server安装教程
Deserialization character escape
PHP-fpm
Powerhouse Cup Preliminary WP
Briefly describe SSRF
连接云服务器Docker中的Mysql 详细图文操作(全)
phpok website vulnerability exploitation analysis
C#中对委托的理解和使用
C#利用开源NPlot实现K线图(蜡烛图)
mysql不是内部或外部命令,也不是可运行的程序或批处理文件解决
Monstache执行monstache -f config.toml出错No processor type exists with name [attachment] [type=parse_exc
awd --waf deployment