【发布时间】:2017-07-28 08:38:52
【问题描述】:
DELETE 的基本语法是
DELETE FROM table
WHERE condition
有没有一种直接的方法在DELETE 语句中使用子查询/别名,如下所示?
DELETE FROM (subquery) as sub
WHERE condition
下面是一个最小的工作表,我尝试使用子查询/别名失败:
---create table
create table orderT (
id integer PRIMARY KEY,
country_code varchar(2),
created_date date,
closed_date date);
---populate table
INSERT INTO orderT VALUES (1, 'US', now(), now() + interval '1 day' * 21);
INSERT INTO orderT VALUES (2, 'CA', now(), now() + interval '1 day' * 35);
--This does not work
DELETE
FROM
(SELECT *
FROM orderT) AS sub
WHERE sub.id = 1;
您可以试试代码here。
PostgreSQL 9.5
【问题讨论】:
-
... In my actual script, the subquery is more 90 lines long.我的超过 300 行! -->> 请不要把你的无知隐藏在令人印象深刻的数字后面。
标签: sql postgresql subquery sql-delete