SQL - 从一个 table 中查找存在于另一个中的记录,如果不存在则删除
SQL - find records from one table which exist in another and delete if not
我有以下四个 SQL tables:
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
我有以下代码将 table 与 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;
我想删除 table 中的所有行,无论 table,在所有 table 中只出现一次。
例如 jkl、stu 和 vwx 只出现一次,所以我想从它们出现的 table 处删除整行。有谁知道该怎么做?
另外,我如何才能删除出现在 table 中且购买日期相同的所有产品?
如果 "delete" 表示不 returning return 它们在 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);
实际上,这会删除仅在 table 上出现的产品,即使它们出现了多次。可以将其调整为仅删除单例,如果这也是必要的。
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;
参见demo。
结果:
表 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
我有以下四个 SQL tables:
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
我有以下代码将 table 与 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;
我想删除 table 中的所有行,无论 table,在所有 table 中只出现一次。
例如 jkl、stu 和 vwx 只出现一次,所以我想从它们出现的 table 处删除整行。有谁知道该怎么做? 另外,我如何才能删除出现在 table 中且购买日期相同的所有产品?
如果 "delete" 表示不 returning return 它们在 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);
实际上,这会删除仅在 table 上出现的产品,即使它们出现了多次。可以将其调整为仅删除单例,如果这也是必要的。
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;
参见demo。
结果:
表 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