将这两个SQL语句合并为一个

问题描述 投票:0回答:2

我有两个表t1,t2,其列(名称,发票,总数)

我正在使用此查询来获取T2中而不是T1中的行

select * from t2  where name=(select source from info) except (select * from t1)

它可以正常工作并返回几行,我要在一个语句中删除这些返回的行。我已经尝试过了,但是它不仅删除了查询中返回的行,还删除了T2中的所有行。

delete from t2 where exists ((select * from t2  where name=(select source from info) except (select * from t1) )) 

以下是我的数据示例:T1enter image description here

T2enter image description here

返回的数据(名称为C2的T1中存在,而不存在于T2中)enter image description here预先感谢。

sql sql-delete sql-except
2个回答
0
投票

您通过正确的查询进入了正确的轨道:

DELETE
FROM table2 t2
WHERE EXISTS (SELECT 1 FROM info i WHERE i.source = t2.name) AND
      NOT EXISTS (SELECT 1 FROM table1 t1 WHERE t1.name = t2.name);

0
投票

我正在使用此查询来获取T2中而不是T1中的行

我将使用的查询是:

select t2.*
from t2
where not exists (select 1 from t1 where t1.? = t2.name);

不清楚t1中匹配列的名称是什么。

很容易变成delete

delete from t2
where not exists (select 1 from t1 where t1.? = t2.name);
© www.soinside.com 2019 - 2024. All rights reserved.