当前位置:网站首页>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 !
边栏推荐
- Service API version design and Practice
- Start learning simple JS
- JMeter pressure measuring tool beginner level chapter
- Introduction to tcapulusdb lossless relocation
- Log4j has been exposed to a nuclear bomb level vulnerability, and the developer has fried the pot!
- December 14, 2021: rebuild the queue according to height. Suppose there's a bunch of people out of order
- AAAI 2022 | Tencent Youtu 14 papers were selected, including image coloring, face security, scene text recognition and other frontier fields
- Remember a compose version of Huarong Road, you deserve it!
- 反序列化——php反序列化
- Mysql中的触发器定义及语法介绍
猜你喜欢

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

游戏安全丨喊话CALL分析-写代码

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

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

The technical design and practice of decrypting the red envelopes of Tiktok Spring Festival

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

Opengauss Developer Day 2022 was officially launched to build an open source database root community with developers

Game security - call analysis - write code
PostgreSQL怎么创建分区表详解

Pourquoi une seule valeur apparaît - elle sur votre carte de données?
随机推荐
How to set the search bar of website construction and what should be paid attention to when designing the search box
[tcapulusdb knowledge base] insert data example (TDR table)
What are the application flow restrictions of API gateway framework?
How to access the top-level domain name and automatically jump to the secondary domain name?
运维故障经历分享
How to use phpMyAdmin to restore a backed up MySQL database
2021-12-10: which can represent a 64 bit floating point number or a 64 bit signed integer
How to create a virtual server through a fortress machine? What are the functions of the fortress machine?
Section 29 basic configuration case of Tianrongxin topgate firewall
Section 30 high availability (HA) configuration case of Tianrongxin topgate firewall
Problem solving: inittramfs unpacking failed:decoding failed
Get and post are nothing more than TCP links in nature?
蚂蚁获FinQA竞赛冠军,在长文本数值推理AI技术上取得突破
Reconstruct the backbone of the supply chain and realize lean production in the LED lighting industry
openGauss Developer Day 2022正式开启,与开发者共建开源数据库根社区
FTP server setup setting website information can I set up FTP myself
API gateway monitoring function the importance of API gateway
AAAI 2022 | Tencent Youtu 14 papers were selected, including image coloring, face security, scene text recognition and other frontier fields
反序列化——php反序列化
How to use data warehouse to create time series