当前位置:网站首页>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
边栏推荐
- Small application project works WeChat integral mall small program of graduation design (4) the opening report of finished product
- OpenFeign implementation downgrade
- 星光不问赶路人!武汉校区小姐姐三个月成功转行软件测试,收获9k+13薪!
- Nacos achieves high availability
- spicy(一)基本定义
- 小程序毕设作品之微信二手交易小程序毕业设计成品(4)开题报告
- 小程序毕设作品之微信二手交易小程序毕业设计成品(7)中期检查报告
- [Driver] udev sets the owner, group and permissions after GPIO is loaded
- Pytorch框架学习记录5——DataLoader的使用
- Process priority nice
猜你喜欢

Redis "super explanation!!!!!!"

How to solve the error "no such file or directory" when EasyCVR starts?

小程序毕设作品之微信二手交易小程序毕业设计成品(3)后台功能

Hystrix service circuit breaker

How does the AI intelligent security video platform EasyCVR configure the simultaneous transmission of audio and video?

小程序毕设作品之微信积分商城小程序毕业设计成品(4)开题报告

一起来学习flutter 的布局组件

使命、愿景、价值观到底有什么区别

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (5) Task Book

redis分布式锁的原子保证
随机推荐
MySQ deadlock
mysql 结构、索引详解
Transformation of traditional projects
Taobao/Tmall get Taobao store details API
day10--install mysql on linux
cv2.polylines
Pytorch framework learning record 7 - convolutional layer
Flutter records and learns different animations (1)
2022-07-29 Group 4 Self-cultivation class study notes (every day)
小程序毕设作品之微信积分商城小程序毕业设计成品(6)开题答辩PPT
Anti-shake and throttling
Roperties class configuration file & DOS to view the host network situation
Wechat second-hand transaction small program graduation design finished product (1) Development overview
SDL player in action
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Product (2) Mini Program Function
【C进阶】数组传参与函数指针
写给技术人的管理入门知识1:什么是管理
Microservice CAP Principles
Resampling a uniformly sampled signal
[Switch] Protocol-Oriented Programming in Swift: Introduction