当前位置:网站首页>Usage of exists in sql
Usage of exists in sql
2022-07-30 03:59:00 【emgexgb_sef】
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;
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢

Hystrix service circuit breaker

Eureka Registry

新型LaaS协议Elephant Swap给ePLATO提供可持续溢价空间

为什么突然间麒麟 9000 5G 版本,又有库存了?

星光不问赶路人!武汉校区小姐姐三个月成功转行软件测试,收获9k+13薪!

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (4) Opening Report

OpenFeign实现负载均衡

Organizations Going Online: A New Trend in Organizational Digital Transformation

Redis "super explanation!!!!!!"

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template
随机推荐
What is the difference between mission, vision and values?
(redistribute, special comprehensive experiment ospf area)
sublime text 3 settings
Pytorch框架学习记录7——卷积层
New interface - API interface for "Taote" keyword search
redis分布式锁的原子保证
Pytorch framework learning record 1 - Dataset class code combat
小程序毕设作品之微信积分商城小程序毕业设计成品(4)开题报告
FreeRTOS Personal Notes - Memory Management
How to solve the error "no such file or directory" when EasyCVR starts?
小程序毕设作品之微信二手交易小程序毕业设计成品(7)中期检查报告
Taobao/Tmall get the list of sold product orders API
Nacos cluster partition
Send it to your friends and let TA treat you to fried chicken!
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (3) Background Functions
Pytorch框架学习记录6——torch.nn.Module和torch.nn.functional.conv2d的使用
进程优先级 nice
Process priority nice
使命、愿景、价值观到底有什么区别
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (7) Interim Inspection Report