当前位置:网站首页>SQL Server database in clause and exists clause conversion
SQL Server database in clause and exists clause conversion
2022-04-23 19:08:00 【Little brother】
One thing I've been confused about before is how to use EXISTS To replace IN Well , What is the meaning of the two ? Today, I will record my personal understanding
IN Scope of representation , It means that a field is within a certain range , This scope generally uses subqueries to get , Thus we can see that IN The result returned by the subquery should be this range set .
EXISTS Indicates presence , There is at least one place , This condition is made by EXISTS Subquery to complete , But here EXISTS The result returned by the subquery is no longer a result set , It's a Boolean value (true or false), In fact, this is quite understandable ,EXISTS It means that if the subquery can find the value, it will return true, execute EXISTS The previous statement .
Take a chestnut
If there is a watch user, It has two fields id and name, We need to check the name with a User information for :
The simplest SQL:select * from user where name like '%a%';
Use IN Of SQL:select u.* from user u where u.id in (select uu.id from user uu where uu.name like '%a%');
We will now use IN Of SQL Change to use EXISTS Of SQL How to write ?
In the beginning, I directly put u.id in Replace with EXISTS, Get the following statement :
select u.* from user u where exists(select uu.id from user uu where uu.name like '%a%');
After testing, it is found that the output result is wrong , This statement queries all users without omission , I believe you also found the problem , Later, I modified the above sentence as follows :
select u.* from user u where exists (select uu.id from user uu where uu.name like '%a%' and uu.id=u.id);
As you can see , Just add... To the subquery “and uu.id=u.id”, The query result is correct .
So why ?
summary :EXISTS Subquery can be regarded as an independent query system , Just to get true and false logical values ,EXISTS The tables of subquery and external query are two completely independent and unrelated tables ( When... In the second table name It contains a Your name exists , Then perform the operation of querying all users in the first table ), When we add id After Association ,EXISTS The tables of sub query and external query are unified , It is a virtual table formed by the combination of the two , It's the same table ( In this way, when the sub query finds the current row in the virtual table uu.name Contained in the a when , The corresponding in the current row of the virtual table u.id And u.name Query to )
So the focus of everything is on this ID Above Association , add to ID relation , The database will first pass the two tables through ID Associations are combined into a virtual table , All query operations are completed on this virtual table , The operation is the same table , Of course, it won't happen as before !
版权声明
本文为[Little brother]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210601423248.html
边栏推荐
- 【玩转Lighthouse】腾讯云轻量服务器搭建全平台视频解析视频下载网站
- Click the input box to pop up the keyboard layout and move up
- Simple use of navigation in jetpack
- 从技术体系到商业洞察,中小研发团队架构实践之收尾篇
- c1000k TCP 连接上限测试1
- 网络协议之:sctp流控制传输协议
- 2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
- RPM package management
- One of the reasons why the WebView web page cannot be opened (and some WebView problem records encountered by myself)
- SSDB基础1
猜你喜欢
Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
2022.04.23(LC_714_买卖股票的最佳时机含手续费)
浅谈c语言指针的强制转换
2022.04.23(LC_763_划分字母区间)
Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
解决:cnpm : 無法加載文件 ...\cnpm.ps1,因為在此系統上禁止運行脚本
JVM的类加载过程
七、DOM(下) - 章节课后练习题及答案
Druid SQL和Security在美团点评的实践
剑指 Offer II 116. 省份数量-空间复杂度O(n),时间复杂度O(n)
随机推荐
SSDB基础2
The fifth bullet of MySQL learning -- detailed explanation of transaction and its operation characteristics
MySQL学习第五弹——事务及其操作特性详解
七、DOM(下) - 章节课后练习题及答案
static类变量快速入门
ESP32 LVGL8. 1 - input devices (input devices 18)
Yyds dry goods inventory stringprep --- Internet string preparation
Go 语言 GUI 框架 fyne 中文乱码或者不显示的问题
Seata handles distributed transactions
MySQL Téléchargement et installation de la version Linux
Nacos cluster construction and MySQL persistence configuration
ESP32 LVGL8. 1. Detailed migration tutorial of m5stack + lvgl + IDF (27)
FTP, SSH Remote Access and control
: app: transformclasseswithrobustfordevrease meituan hot repair compilation error record
An 8266 crash
Minesweeping II of souI instance
Sogou cell thesaurus analysis (only extract words and word frequency)
[play with lighthouse] Tencent cloud lightweight server builds a full platform video analysis video download website
Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
The corresponding permissions required to automatically open the app in the setting interface through accessibility service