当前位置:网站首页>Usage of exists in sql
Usage of exists in sql
2022-07-31 10:17:00 【xiaoweiwei99】
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 Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Ali 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
边栏推荐
猜你喜欢
第六章
尚医通【预约挂号系统】总结
Come n times - 07. Rebuild the binary tree
Day113.尚医通:用户认证、阿里云OSS、就诊人管理
如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
darknet 训练分类网络
Gradle series - Groovy overview, basic use (based on Groovy document 4.0.4) day2-1
Redis Sentinel原理
实现线程池
随机推荐
Redis Cluster - Sentinel Mode Principle (Sentinel)
Meikle Studio--Hongmeng 14-day development training notes (8)
Web系统常见安全漏洞介绍及解决方案-XSS攻击
SQL存储过程详解
出色的移动端用户验证
odoo14 | 附件上传功能及实际使用
我们能做出来数据库吗?
Qt compile error: C2228: '.key' must have class/struct/union on the left
如何优雅的停止一个线程?
nodeJs--url模块
Business-(Course-Chapter-Subsection) + Course Publishing Some Business Ideas
半个月时间把MySQL重新巩固了一遍,梳理了一篇几万字 “超硬核” 文章!
NowCoderTOP28-34二叉树——持续更新ing
双链表的创建
业务-(课程-章节-小节)+课程发布一些业务思路
通过栗子来学习MySQL高级知识点(学习,复习,面试都可)
Chapter Six
loadrunner脚本--添加检查点
【GORM】存取数组/自定义类型数据
Come n times - 07. Rebuild the binary tree