如何在postgresql中的with子句之后使用delete子句?
How to use delete clause after with clause in postgresql?
查询 1:
with table1 as (select * from table2) delete * from table1;
查询 2:
with table1 as (select * from table2) delete * from table1 where col1 = 'something';
以上两个查询执行后都返回错误?有人可以帮我吗?
根据评论区的问题陈述,您可以使用下面的查询来删除重复项
delete from rohilla a using rohilla b where a=b and a.ctid < b.ctid;
使用 with 子句,您可以执行以下操作来删除重复项。 (如果整行重复,下面的 Col1 可以是任何列)
WITH x AS
(
SELECT col1,
Min(ctid) AS min
FROM rohilla
GROUP BY col1
HAVING Count(col1) > 1 )
DELETE
FROM rohilla b
using x
WHERE x.col1 = b.col1
AND x.min <> b.ctid;
您不能从 Postgres 的 CTE 中删除。显然,我相信您知道您可以这样做:
delete from table2
where col1 = 'something';
如果你想涉及 CTE,那么你可以使用某种过滤,通常是在主键上:
with table1 as (
select * from table2
)
delete from table2 t2 using
table1 t1
where t1.<primary key> = t2.<primary key> and
t1.col1 = 'something';
查询 1:
with table1 as (select * from table2) delete * from table1;
查询 2:
with table1 as (select * from table2) delete * from table1 where col1 = 'something';
以上两个查询执行后都返回错误?有人可以帮我吗?
根据评论区的问题陈述,您可以使用下面的查询来删除重复项
delete from rohilla a using rohilla b where a=b and a.ctid < b.ctid;
使用 with 子句,您可以执行以下操作来删除重复项。 (如果整行重复,下面的 Col1 可以是任何列)
WITH x AS
(
SELECT col1,
Min(ctid) AS min
FROM rohilla
GROUP BY col1
HAVING Count(col1) > 1 )
DELETE
FROM rohilla b
using x
WHERE x.col1 = b.col1
AND x.min <> b.ctid;
您不能从 Postgres 的 CTE 中删除。显然,我相信您知道您可以这样做:
delete from table2
where col1 = 'something';
如果你想涉及 CTE,那么你可以使用某种过滤,通常是在主键上:
with table1 as (
select * from table2
)
delete from table2 t2 using
table1 t1
where t1.<primary key> = t2.<primary key> and
t1.col1 = 'something';