Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use a SQL statement to remove the same data from two tables and accumulate the same data as the third table

Shulou Source: shulou.com Published: 2022-05-31 17:39:20 10月02日 Update

This article mainly explains "how to use a SQL statement to remove the same data in two tables and then accumulate the same data in the third table". The content in the article is simple and clear, and it is easy to learn and understand. below, please follow the editor's train of thought to study and learn "how to use a SQL statement to remove the same data in two tables and accumulate the same data in the third table".

Table X:

A B C D

-

Beijing Guangzhou 1000 10

Beijing and Shenzhen 1200 20

Beijing and Shanghai 500 30

Beijing Tianjin 300 40

Beijing Henan 800 20

Table Y:

A B

-

Beijing and Shenzhen

Beijing and Shanghai

Table Z:

A B C D

-

Beijing Guangzhou 200 10

Beijing and Shanghai-100-10

Beijing Sichuan 800 50

Objective: to remove the data with the same keyword in Table Y from Table X, and add the data in Table Z to accumulate the same keyword (AMagi B is the keyword).

The results are as follows:

A B C D

-

Beijing Guangzhou 1200 20

Beijing and Shanghai 400 20

Beijing Henan 800 20

Beijing Sichuan 800 50

The copy code is as follows:

Select an as d from bstatement sum (c) as c

(select * from x union all select * from z) t

Where not exits (select 1 from y where y.a=t.an and y.b=t.b)

Group by a,b

Thank you for your reading, the above is "how to use a SQL statement to remove the same data in two tables and then accumulate the same data in the third table". After the study of this article, I believe you have a deeper understanding of how to use a SQL statement to remove the same data in two tables and accumulate the same data in the third table. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Beijing same data three two sentence Shanghai keyword Guangzhou learning code content Sichuan Henan Shenzhen that is train of thought situation article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Information Microsoft Shulou Tech Info Xiaomi