当前位置:网站首页>Detailed usage of exists in SQL statements
Detailed usage of exists in SQL statements
2022-06-23 22:54:00 【1024 Q】
Preface
One 、 Build table
Two 、 stay SELECT Use in statement EXISTS
1. stay SQL Use in EXISTS
2. stay SQL Use in NOT EXISTS
3. stay SQL Using multiple in NOT EXISTS
4. stay SQL Using multiple in EXISTS
5. stay SQL Use in NOT EXISTS and EXISTS
3、 ... and 、 stay DELETE Use in statement EXISTS
1. stay MySQL Use in
2. stay Oracle Use in
Four 、 stay UPDATE Use in statement EXISTS
1. stay MySQL Use in
2. stay Oracle Use in
summary
PrefaceIn the course of business , There will be similar needs .
demand 1:UPDATE surface TEST_TB01 Records in ; Meet the conditions : These records are not in TEST_TB02 in .
demand 2:UPDATE surface TEST_TB01 Records in ; Meet the conditions : These are recorded in TEST_TB02 in .
stay SQL In the sentence EXISTS Usage of , Can be relatively simple to solve such needs .
One 、 Build table1. stay MySQL Database table creation statement
CREATE TABLE TEST_TB01( sensor_id BIGINT, part_id BIGINT, flag VARCHAR(64) )COMMENT ' Data sheet 1 ';CREATE TABLE TEST_TB02( sensor_id BIGINT, part_id BIGINT, flag VARCHAR(64) )COMMENT ' Data sheet II ';CREATE TABLE TEST_TB03( sensor_id BIGINT, part_id BIGINT, flag VARCHAR(64) )COMMENT ' Data sheet III ';2. stay ORACLE Database table creation statement
CREATE TABLE TEST_TB01( sensor_id NUMBER(16), part_id NUMBER(16), flag VARCHAR(64) );CREATE TABLE TEST_TB02( sensor_id NUMBER(16), part_id NUMBER(16), flag VARCHAR(64) ); Two 、 stay SELECT Use in statement EXISTSstay SELECT Of SQL Use in statement EXISTS.
stay TEST_TB01 insert data :
INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2101,8811,' Xiamen ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2102,8812,' quanzhou ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2103,8813,' fuzhou ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2104,8814,' zhangzhou ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2105,8815,' Hangzhou ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2106,8816,' Shanghai ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2107,8817,' Beijing ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2108,8818,' Shenzhen ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2109,8819,' China ');stay TEST_TB02 insert data :
INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2101,8811,' Xiamen ');INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2102,8812,' quanzhou ');INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2103,8813,' fuzhou ');INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2104,8814,' zhangzhou ');INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2109,8819,' China ');stay TEST_TB03 insert data :
INSERT INTO TEST_TB03 (sensor_id,part_id,flag) VALUES(2106,8816,' Shanghai ');INSERT INTO TEST_TB03 (sensor_id,part_id,flag) VALUES(2107,8817,' Beijing ');INSERT INTO TEST_TB03 (sensor_id,part_id,flag) VALUES(2109,8819,' China ');see TEST_TB01 data :

see TEST_TB02 data :

see TEST_TB03 data :

demand : from TEST_TB01 Find out in TEST_TB02 Records that exist in , The association condition is the of two tables sensor_id equal .
SQL sentence :
SELECT aa.sensor_id,aa.part_id,aa.flagFROM TEST_TB01 aaWHERE EXISTS (SELECT 1 FROM TEST_TB02 bb WHERE aa.sensor_id = bb.sensor_id);Execution results :

demand : from TEST_TB01 Find out in TEST_TB02 Records that do not exist in , The association condition is the of two tables sensor_id equal .
SQL sentence :
SELECT aa.sensor_id,aa.part_id,aa.flagFROM TEST_TB01 aaWHERE NOT EXISTS (SELECT 1 FROM TEST_TB02 bb WHERE aa.sensor_id = bb.sensor_id);Execution results :

demand : from TEST_TB01 Find out in TEST_TB02 and TEST_TB03 Records that do not exist in , The association condition is the of the table sensor_id equal .
SQL sentence :
SELECT aa.sensor_id,aa.part_id,aa.flagFROM TEST_TB01 aaWHERE NOT EXISTS (SELECT 1 FROM TEST_TB02 bb WHERE aa.sensor_id = bb.sensor_id) AND NOT EXISTS (SELECT 1 FROM TEST_TB03 cc WHERE aa.sensor_id = cc.sensor_id);Execution results :

demand : from TEST_TB01 Find out in TEST_TB02 and TEST_TB03 Records that exist in , The association condition is the of the table sensor_id equal .
SQL sentence :
SELECT aa.sensor_id,aa.part_id,aa.flagFROM TEST_TB01 aaWHERE EXISTS (SELECT 1 FROM TEST_TB02 bb WHERE aa.sensor_id = bb.sensor_id) AND EXISTS (SELECT 1 FROM TEST_TB03 cc WHERE aa.sensor_id = cc.sensor_id);Execution results :

demand : from TEST_TB01 Find out in TEST_TB02 There is, but TEST_TB03 Records that do not exist in , The association condition is the of the table sensor_id equal .
SQL sentence :
SELECT aa.sensor_id,aa.part_id,aa.flagFROM TEST_TB01 aaWHERE EXISTS (SELECT 1 FROM TEST_TB02 bb WHERE aa.sensor_id = bb.sensor_id) AND NOT EXISTS (SELECT 1 FROM TEST_TB03 cc WHERE aa.sensor_id = cc.sensor_id);Execution results :

stay DELETE Of SQL Use in statement EXISTS and NOT EXISTS.
stay TEST_TB01 insert data :
INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2101,8811,' Xiamen ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2102,8812,' quanzhou ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2103,8813,' fuzhou ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2104,8814,' zhangzhou ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2105,8815,' Hangzhou ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2106,8816,' Shanghai ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2107,8817,' Beijing ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2108,8818,' Shenzhen ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2109,8819,' China ');stay TEST_TB02 insert data :
INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2101,8811,' Xiamen ');INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2102,8812,' quanzhou ');INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2103,8813,' fuzhou ');INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2104,8814,' zhangzhou ');INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2109,8819,' China ');1. stay MySQL Use in demand : from TEST_TB01 Delete in TEST_TB02 Records that exist in , The association condition is the of two tables sensor_id equal .
Be careful : This example USES MySQL edition :MySQL 5.7.33.
SQL sentence :
DELETE FROM TEST_TB01 aaWHERE EXISTS (SELECT 1 FROM TEST_TB02 bb WHERE aa.sensor_id = bb.sensor_id);Execution results :

Conclusion : stay MySQL It is not supported in DELETE Of SQL Use in statement EXISTS and NOT EXISTS This syntax .( This example version :MySQL 5.7.33).
Address this need :
SQL sentence :
DELETE aaFROM TEST_TB01 aa INNER JOIN TEST_TB02 bb ON aa.sensor_id = bb.sensor_id;Be careful : stay SQL in DELETE Followed by the alias of the table name to be deleted in the requirement .
If the alias is not used, an error will be reported :

demand : from TEST_TB01 Delete in TEST_TB02 Records that exist in , The association condition is the of two tables sensor_id equal .
SQL sentence :
DELETE FROM TEST_TB01 aaWHERE EXISTS (SELECT 1 FROM TEST_TB02 bb WHERE aa.sensor_id = bb.sensor_id);Execution results :
Before execution TEST_TB01:
Before execution TEST_TB02:

After execution TEST_TB01:

stay UPDATE Of SQL Use in statement EXISTS.
stay TEST_TB01 insert data :
INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2101,8811,' City ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2102,8812,' City ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2103,8813,' City ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2104,8814,' City ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2105,8815,' City ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2106,8816,' City ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2107,8817,' City ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2108,8818,' City ');INSERT INTO TEST_TB01 (sensor_id,part_id,flag) VALUES(2109,8819,' City ');stay TEST_TB02 insert data :
INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2101,8811,' Xiamen ');INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2102,8812,' quanzhou ');INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2103,8813,' fuzhou ');INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2104,8814,' zhangzhou ');INSERT INTO TEST_TB02 (sensor_id,part_id,flag) VALUES(2109,8819,' China ');1. stay MySQL Use in demand : stay TEST_TB01 In the update , stay TEST_TB02 Records that exist in , The association condition is the of two tables sensor_id equal .
Be careful : This example USES MySQL edition :MySQL 5.7.33.
SQL sentence :
UPDATE TEST_TB01 aa SET (aa.part_id, aa.flag) = (SELECT bb.part_id, bb.flag FROM TEST_TB02 bb WHERE aa.sensor_id = bb.sensor_id) WHERE EXISTS (SELECT 1 FROM TEST_TB02 cc WHERE aa.sensor_id = cc.sensor_id);Execution results :

Conclusion : stay MySQL It is not supported in UPDATE Of SQL Use in statement EXISTS and NOT EXISTS This syntax .( This example version :MySQL 5.7.33).
Address this need :
SQL sentence :
UPDATE TEST_TB01 aa ,TEST_TB02 bbSET aa.part_id=bb.part_id, aa.flag=bb.flagWHERE aa.sensor_id = bb.sensor_id;Execution results :
Before execution TEST_TB01:

Before execution TEST_TB02:

After execution TEST_TB01:

demand : stay TEST_TB01 In the update , stay TEST_TB02 Records that exist in , The association condition is the of two tables sensor_id equal .
SQL sentence :
UPDATE TEST_TB01 aa SET (aa.part_id, aa.flag) = (SELECT bb.part_id, bb.flag FROM TEST_TB02 bb WHERE aa.sensor_id = bb.sensor_id) WHERE EXISTS (SELECT 1 FROM TEST_TB02 cc WHERE aa.sensor_id = cc.sensor_id);Execution results :
Before execution TEST_TB01:

Before execution TEST_TB02:

After execution TEST_TB01:

above , thank .
summaryThis is about SQL In the sentence EXISTS This is the end of the article on usage , More about SQL sentence EXISTS Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !
边栏推荐
- What are the steps required for TFTP to log in to the server through the fortress machine? Operation guide for novice
- Section 30 high availability (HA) configuration case of Tianrongxin topgate firewall
- Notes to nodejs (II)
- 2021-12-10: which can represent a 64 bit floating point number or a 64 bit signed integer
- What server is used for website construction? What is the price of the server
- How to use xshell to log in to the server through the fortress machine? How does the fortress machine configure the tunnel?
- Server classification of hardware knowledge (2)
- The technical design and practice of decrypting the red envelopes of Tiktok Spring Festival
- 专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!
- 在宇宙的眼眸下,如何正确地关心东数西算?
猜你喜欢

Beauty of script │ VBS introduction interactive practice

Why is only one value displayed on your data graph?

C#/VB.NET Word转Text

為什麼你的數據圖譜分析圖上只顯示一個值?

openGauss Developer Day 2022正式开启,与开发者共建开源数据库根社区

【技术干货】蚂蚁办公零信任的技术建设路线与特点

脚本之美│VBS 入门交互实战

Save: software analysis, verification and test platform

专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!

Chaos engineering, learn about it
随机推荐
How to set ulimit value for SYSTEMd service in easycvr?
How to use FTP to upload websites to the web
Detailed explanation of flutter exception capture
How to use data warehouse to create time series
The old CVM of Tencent cloud is migrated to the new CVM, and the IP remains unchanged
SLSA: 成功SBOM的促进剂
What are the steps required for TFTP to log in to the server through the fortress machine? Operation guide for novice
[tcapulusdb knowledge base] [generic table] read data interface description
[tcapulusdb knowledge base] update data example (TDR table)
解密抖音春节红包背后的技术设计与实践
How to build a business analysis system
pyspark on hpc
运维故障经历分享
How to use fortress remote server two types of Fortress
December 14, 2021: rebuild the queue according to height. Suppose there's a bunch of people out of order
Advantages of micro service registry Nacos over Eureka
Slsa: accelerator for successful SBOM
如何利用数仓创建时序表
Detailed explanation of bitmap optimization
Save: software analysis, verification and test platform