如何从两个表中删除与postgresql中的单个查询相同的行
How to delete rows from two tables which are in common with single query in postgresql
我有两个 tables t1 和 t2
table t1如下:
id name
1 x
2 y
3 z
table t2如下:
id name
1 a
121 b
131 c
在这里,我选择了 table 中常见的行,即
SELECT *
from t1,t2
where t1.id=t2.id;
现在我想同时删除两个 table 中 id=1 的行。我试图删除行,但我只能删除一个 table 而不是两个。谁能帮我解决这个问题。
你可以用 data modifying common table expression
with common_ids as (
select id
from t1
intersect
select id
from t2
), t1_delete as (
delete from t1
where id in (select id from common_ids)
)
delete from t2
where id in (select id from common_ids);
我有两个 tables t1 和 t2
table t1如下:
id name
1 x
2 y
3 z
table t2如下:
id name
1 a
121 b
131 c
在这里,我选择了 table 中常见的行,即
SELECT *
from t1,t2
where t1.id=t2.id;
现在我想同时删除两个 table 中 id=1 的行。我试图删除行,但我只能删除一个 table 而不是两个。谁能帮我解决这个问题。
你可以用 data modifying common table expression
with common_ids as (
select id
from t1
intersect
select id
from t2
), t1_delete as (
delete from t1
where id in (select id from common_ids)
)
delete from t2
where id in (select id from common_ids);