将这两个 SQL 语句合并为一个
Combine these two SQL statements into one
我有两个 tables t1
, t2
列 name, invoice, total
.
我正在使用此查询获取 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)
))
这是我的数据示例:
T1
T2
返回的数据(存在于 T2 而不是 T1 中名称为 C2 的地方)
第 3 个 table 信息是获取名称,在本例中是 C2。
提前致谢。
I am using this query to get the rows that in T2 and not in 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);
您可以通过 t2
到 t1
的左连接来实现:
delete t
from (
select * from t2
where name = (select source from info)
) t left join t1
on t1.name = t.name and t1.invoice = t.invoice and t1.total = t.total
where t1.name is null
参见demo。
如果你想使用 NOT EXISTS
:
delete t
from (
select * from t2
where name = (select source from info)
) t
where not exists (
select 1 from t1
where name = t.name and invoice = t.invoice and total = t.total
)
参见demo。
结果(t2
中剩余的行):
> name | invoice | total
> :--- | ------: | ----:
> C1 | 1 | 150
> C1 | 2 | 300
> C2 | 1 | 200
> C2 | 2 | 165
您想从 t2 中删除有两个条件:
- 姓名=(select来源信息)
- 行在 t1 中没有匹配项
声明:
delete from t2
where name = (select source from info)
and not exists
(
select *
from t1
where t1.name = t2.name
and t1.invoice = t2.invoice
and t1.total = t2.total
);
或更短的 IN
子句,仅在允许 IN
和元组的 DBMS 中可用:
delete from t2
where name = (select source from info)
and (name, invoice, total) not in (select name, invoice, total from t1);
我有两个 tables t1
, t2
列 name, invoice, total
.
我正在使用此查询获取 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)
))
这是我的数据示例:
T1
T2
返回的数据(存在于 T2 而不是 T1 中名称为 C2 的地方)
第 3 个 table 信息是获取名称,在本例中是 C2。
提前致谢。
I am using this query to get the rows that in T2 and not in 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);
您可以通过 t2
到 t1
的左连接来实现:
delete t
from (
select * from t2
where name = (select source from info)
) t left join t1
on t1.name = t.name and t1.invoice = t.invoice and t1.total = t.total
where t1.name is null
参见demo。
如果你想使用 NOT EXISTS
:
delete t
from (
select * from t2
where name = (select source from info)
) t
where not exists (
select 1 from t1
where name = t.name and invoice = t.invoice and total = t.total
)
参见demo。
结果(t2
中剩余的行):
> name | invoice | total
> :--- | ------: | ----:
> C1 | 1 | 150
> C1 | 2 | 300
> C2 | 1 | 200
> C2 | 2 | 165
您想从 t2 中删除有两个条件:
- 姓名=(select来源信息)
- 行在 t1 中没有匹配项
声明:
delete from t2
where name = (select source from info)
and not exists
(
select *
from t1
where t1.name = t2.name
and t1.invoice = t2.invoice
and t1.total = t2.total
);
或更短的 IN
子句,仅在允许 IN
和元组的 DBMS 中可用:
delete from t2
where name = (select source from info)
and (name, invoice, total) not in (select name, invoice, total from t1);