我有两个表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) ))
您通过正确的查询进入了正确的轨道:
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);
我正在使用此查询来获取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);