当前位置:网站首页>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 !
边栏推荐
- Ranking of high cost performance commercial endowment insurance products in 2022
- SLSA: 成功SBOM的促进剂
- Remember a compose version of Huarong Road, you deserve it!
- How to set the text style as PG in website construction
- 蚂蚁获FinQA竞赛冠军,在长文本数值推理AI技术上取得突破
- What are the steps required for TFTP to log in to the server through the fortress machine? Operation guide for novice
- Notes to nodejs (III)
- sql server常用sql
- How to set up a website construction map
- Production of labels for table products
猜你喜欢

Beauty of script │ VBS introduction interactive practice

蚂蚁集团自研TEE技术通过国家级金融科技产品认证

Section 29 basic configuration case of Tianrongxin topgate firewall

C#/VB.NET Word转Text

Game security - call analysis - write code

In the eyes of the universe, how to correctly care about counting East and West?

Section 30 high availability (HA) configuration case of Tianrongxin topgate firewall

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

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

Chaos engineering, learn about it
随机推荐
How to shut down the server in the fortress machine? What other operations can the fortress machine perform?
【技术干货】蚂蚁办公零信任的技术建设路线与特点
PostgreSQL怎么创建分区表详解
Remember a compose version of Huarong Road, you deserve it!
Detailed explanation of bitmap optimization
Consequences of website construction without SSL authentication are websites without SSL authentication reliable
openGauss Developer Day 2022正式开启,与开发者共建开源数据库根社区
Notes to nodejs (II)
Flush cache clear
How to set secondary title in website construction what is the function of secondary title
Micro build low code tutorial -hello, world
Detailed explanation of GC principle
How to set dynamic background for website construction what are the benefits of dynamic background
Service API version design and Practice
Virtual machine performance monitoring and fault handling commands on the console
How to set the website construction title bar drop-down
反序列化——php反序列化
SQL Server common SQL
Operation and maintenance failure experience sharing
Docker中部署Redis集群与部署微服务项目的详细过程