How to use a SQL statement to remove the same data from two tables and accumulate the same data as the third table
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!