35

我想根据引用第一个的另一个表上存在的数据从一个表中删除,但是,我的代码可以工作并显示当我将其作为 SELECT 语句运行时要删除的值,但是当我将其更改为DELETE 它给了我错误,我不明白他们为什么在那里。

DELETE leadCustomer.* FROM coursework.leadCustomer LEFT JOIN coursework.flightBooking
ON leadCustomer.customerID = flightBooking.customerID
WHERE leadCustomer.customerID NOT IN (
SELECT customerID FROM (SELECT customerID, status FROM coursework.flightBooking) AS
StatusCount where status IN  ('R','H') GROUP BY customerID
)
AND leadCustomer.customerID = 8;

错误:

ERROR:  syntax error at or near "leadCustomer"
LINE 1: DELETE leadCustomer.* FROM coursework.leadCustomer LEFT JOIN...
               ^

********** Error **********

ERROR: syntax error at or near "leadCustomer"
SQL state: 42601
Character: 8

我正在使用 postgres

4

5 回答 5

81

样本。删除表 'A' 中的记录 表 'H' 中没有记录

DELETE A FROM ARTICULO_ALMACEN A
LEFT JOIN HISTORICO_UNION H
ON A.COD_ARTICULO = H.COD_ARTICULO
AND A.COD_ALMACEN = H.COD_ARTICULO_ALMACEN
AND A.TPROPIEDAD1 = H.PROPIEDAD1
AND A.TPROPIEDAD2 = H.PROPIEDAD2
AND A.TPROPIEDAD3 = H.PROPIEDAD3
WHERE H.COD_ARTICULO IS NULL
于 2014-11-25T12:22:53.187 回答
21

从我看到的地方,您实际上并不需要加入来执行此操作...

DELETE FROM coursework.leadCustomer 
WHERE leadCustomer.customerID NOT IN (
SELECT distinct customerID FROM coursework.flightBooking  where status IN  ('R','H') 
)
AND leadCustomer.customerID = 8;

它将删除leadcustomer 中的所有记录,其customerID 为:1)不同于8 2)不在表航班预订中,状态为“R”或“H”

这不是你想要做的吗?

于 2013-03-20T11:22:20.313 回答
1

您将需要这样做:

从 TableA 中删除 ID in(从 tableA 中选择 ID a left outer join tableB b on a.ID = b.ID where b.ID is NULL)

于 2014-02-13T22:22:44.887 回答
-4

.*leadCustomer.* 即删除:

DELETE leadCustomer FROM coursework.leadCustomer LEFT JOIN coursework.flightBooking
ON leadCustomer.customerID = flightBooking.customerID
WHERE leadCustomer.customerID NOT IN (
SELECT customerID FROM (SELECT customerID, status FROM coursework.flightBooking) AS
StatusCount where status IN  ('R','H') GROUP BY customerID
)
AND leadCustomer.customerID = 8;
于 2013-03-20T11:22:10.157 回答
-4

you can try this

 DELETE leadCustomer FROM coursework.leadCustomer lc
 LEFT JOIN coursework.flightBooking fb ON lc.customerID = fb.customerID 
 and status IN  ('R','H')and fb.customer_id is not null
 WHERE  leadCustomer.customerID = 8;
于 2013-03-20T11:35:20.183 回答