当前位置:网站首页>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 Work (5) Task Book
- The difference between BGP room and ordinary room in Beijing
- Eureka Registry
- Sentinel Traffic Guard
- 数组和结构体
- 逆向理论知识3【UI修改篇】
- ospf 综合实验(重发布,特殊区域)
- 小程序毕设作品之微信积分商城小程序毕业设计成品(8)毕业设计论文模板
- Has been empty, a straightforward, continue to copy the top off!
- Hystrix 服务熔断
猜你喜欢
Smart answer function, CRMEB knowledge payment system must have!
Roperties类配置文件&DOS查看主机网络情况
[ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?
Introduction to management for technical people 1: What is management
Organizations Going Online: A New Trend in Organizational Digital Transformation
一起来学习flutter 的布局组件
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT
forward与redirect的区别
Pytorch framework learning record 3 - the use of Transform
Pytorch框架学习记录4——数据集的使用(torchvision.dataset)
随机推荐
Flutter record learning different animation (2)
Eureka注册中心
小程序毕设作品之微信积分商城小程序毕业设计成品(3)后台功能
ospf 综合实验(重发布,特殊区域)
AI智能安防视频平台EasyCVR如何配置音视频同时传输?
小程序毕设作品之微信二手交易小程序毕业设计成品(7)中期检查报告
小程序毕设作品之微信积分商城小程序毕业设计成品(1)开发概要
forward与redirect的区别
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (7) Interim Inspection Report
Wechat second-hand transaction small program graduation design finished product (1) Development overview
Pytorch框架学习记录6——torch.nn.Module和torch.nn.functional.conv2d的使用
route filter
Introduction to management for technical people 1: What is management
Alibaba search new product data API by keyword
LoadBalancer load balancing
小程序毕设作品之微信积分商城小程序毕业设计成品(8)毕业设计论文模板
spicy(二)unit hooks
Microservice CAP Principles
OpenFeign implementation downgrade
Pytorch framework to study record 6 - the torch. Nn. The Module and the torch nn. Functional. The use of conv2d