簡體   English   中英

SQL - 從一個表中查找存在於另一個表中的記錄,如果不存在則刪除

[英]SQL - find records from one table which exist in another and delete if not

我有以下四個 SQL 表:

Table 1:
-----------------------
Product | Date_Purchase
-----------------------
abc     | 06-Jan-19
def     | 05-Jan-18
ghi     | 05-Apr-19
abc     | 06-Feb-19

Table 2:

------------------------
Product | Date_Purchase
------------------------
jkl    | 6-Feb-19
mno    | 2-Aug-18
ghi    | 9-May-19
pqr    | 1-Sep-19

Table 3:

-------------------------
Product | Date_Purchase
-------------------------
ghi    | 2-Aug-18
mno    | 9-May-19
pqr    | 2-Aug-18
abc    | 06-Jan-19

Table 4:

-------------------------
Product | Date_Purchase
-------------------------
stu    | 9-May-19
vwx    | 05-Apr-19
ghi    | 9-May-19
def    | 05-Jan-18

我有以下代碼將表與 Union 連接起來:

SELECT Product, Date_Purchase FROM Table1 UNION ALL
SELECT Product, Date_Purchase FROM Table2 UNION ALL
SELECT Product, Date_Purchase FROM Table3 UNION ALL SELECT Product, Date_Purchase FROM Table4
ORDER BY Product, Date_Purchase;

我想從表中刪除所有行,不管是什么表,在所有表中只出現一次。

例如 jkl、stu 和 vwx 只出現一次,所以我想從它們出現的表中刪除整行。 有誰知道如何做到這一點? 另外,我如何刪除表格中出現的所有產品並具有相同的購買日期?

如果“刪除”意味着不返回在select返回它們,則:

SELECT pd.*
FROM (SELECT pd.*, COUNT(*) OVER (PARTITION BY Product) as cnt
      FROM ((SELECT Product, Date_Purchase FROM Table1
            ) UNION ALL
            (SELECT Product, Date_Purchase FROM Table2
            ) UNION ALL
            (SELECT Product, Date_Purchase FROM Table3
            ) UNION ALL
            (SELECT Product, Date_Purchase FROM Table4
            )
           ) pd
     ) pd
WHERE cnt = 1;

如果“delete”的意思是delete ,那么你需要四個delete語句,每個都像這樣:

delete t
   from table1 t
   where not exists (select 1 from table2 where t2.product = t.product) and
         not exists (select 1 from table3 where t3.product = t.product) and
         not exists (select 1 from table4 where t4.product = t.product);

實際上,這會刪除僅在表中的產品,即使它們多次出現。 如果有必要,可以調整為僅刪除單例。

MySql 的解決方案,您可以在 1 個語句中從所有 4 個表中刪除:

delete t1, t2, t3, t4
from (
  select u.product, count(*) counter 
  from (
    select * from table1 union all
    select * from table2 union all
    select * from table3 union all
    select * from table4
  ) u  
  group by u.product
) t 
left join table1 t1 on t1.product = t.product
left join table2 t2 on t2.product = t.product
left join table3 t3 on t3.product = t.product
left join table4 t4 on t4.product = t.product
where t.counter = 1; 

請參閱演示
結果:

表格1

> Product | Date_Purchase
> :------ | :------------
> abc     | 06-Jan-19    
> def     | 05-Jan-18    
> ghi     | 05-Apr-19    
> abc     | 06-Feb-19  

表2

> Product | Date_Purchase
> :------ | :------------
> mno     | 2-Aug-18     
> ghi     | 9-May-19     
> pqr     | 1-Sep-19 

表3

> Product | Date_Purchase
> :------ | :------------
> ghi     | 2-Aug-18     
> mno     | 9-May-19     
> pqr     | 2-Aug-18     
> abc     | 06-Jan-19 

表4

> Product | Date_Purchase
> :------ | :------------
> ghi     | 9-May-19     
> def     | 05-Jan-18 

僅當產品和日期出現兩次時才嘗試使用 Scratte 的版本(未選中,因為寫在移動設備上):

SELECT pdo.*
FROM (SELECT pd.*, COUNT(*) OVER (PARTITION BY Product) as cnt
      FROM ((SELECT Product, Date_Purchase FROM Table1
            ) UNION ALL
            (SELECT Product, Date_Purchase FROM Table2
            ) UNION ALL
            (SELECT Product, Date_Purchase FROM Table3
            ) UNION ALL
            (SELECT Product, Date_Purchase FROM Table4
            )
           ) pd
     ) pdo
Group by pdo.Product,pdo.Date_Purchase
Having cnt=1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM