SQL-从一个表中查找存在于另一个表中的记录,如果不存在则删除

SQL-从一个表中查找存在于另一个表中的记录,如果不存在则删除,sql,union-all,Sql,Union All,我有以下四个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 -----------------

我有以下四个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 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版本的:


这回答了你的问题吗?您使用的是什么数据库管理系统(Oracle、MySQL等)?我怀疑您能否在一次操作中从所有四个表中删除。您可能需要将要删除的所有项目填充到临时表中,然后执行四次删除(每个源表一次)以删除临时表中的任何项目。您是否解决了问题?那么请让我们知道。福帕斯提供的解决方案有效!谢谢大家如果“在所有表格中只出现一次”包括购买日期怎么办?@Scratte是第一个版本还是第二个版本?@Nikolaus都是。abc | 06-Feb-19(表1)中的abc不会出现在第一页。@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