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

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template

After 5 years of Ali internship interview~

(6) "Digital Electricity" - Diodes and CMOS Gate Circuits (Introduction)

Pytorch框架学习记录3——Transform的使用

Summary of Rpc and gRpc Introduction

Pytorch框架学习记录4——数据集的使用(torchvision.dataset)

函数的底层机制

route filter

弘玑再度入围Gartner 2022 RPA魔力象限并实现位置大幅跃升

Pytorch框架学习记录2——TensorBoard的使用
随机推荐
Nacos achieves high availability
小程序毕设作品之微信积分商城小程序毕业设计成品(5)任务书
AI智能安防视频平台EasyCVR如何配置音视频同时传输?
FreeRTOS Personal Notes - Memory Management
route filter
OA Project Pending Meeting & History Meeting & All Meetings
Nacos cluster partition
Pytorch框架学习记录5——DataLoader的使用
进程优先级 nice
MySQ deadlock
运行时间监控:如何确保网络设备运行时间
flutter 记录学习不一样的动画(二)
Nacos namespace
MySQ死锁
Process priority nice
2022.7.29-----leetcode.593
发给你的好友,让 TA 请你吃炸鸡!
Taobao/Tmall get the list of sold product orders API
RRU、BBU、AAU
Alibaba search new product data API by keyword