2012-08-01 63 views
97
DELETE B.* 
FROM m_productprice B 
     INNER JOIN m_product C ON B.m_product_id = C.m_product_id 
WHERE C.upc = '7094' AND B.m_pricelist_version_id = '1000020' 

我收到以下錯誤PostgreSQL的8.2.11PostgreSQL的刪除與內部聯接

ERROR: syntax error at or near "B" 
LINE 1: DELETE B.* from m_productprice B INNER JOIN m_product C ON ... 

我希望能夠對

DELETE B from m_productprice B INNER JOIN m_product C ON B.... 
ERROR: syntax error at or near "B" 

我希望能夠對

ERROR: syntax error at or near "INNER" 
LINE 1: DELETE from m_productprice B INNER JOIN m_product C ON B.m_... 

是什麼我的查詢的問題?

+2

8.2?你應該儘快升級。該版本不再受支持。請閱讀手冊:DELETE語句沒有'INNER JOIN':http://www.postgresql.org/docs/8.2/static/sql-delete.html – 2012-08-01 06:53:31

+0

任何執行此查詢的方法加入 – dude 2012-08-01 06:55:38

+0

請參閱手冊,有一個例子完全。 – 2012-08-01 06:59:09

回答

37

這爲我工作:

DELETE from m_productprice 
WHERE m_pricelist_version_id='1000020' 
     AND m_product_id IN (SELECT m_product_id 
          FROM m_product 
          WHERE upc = '7094'); 
+0

這也適用於我。乾杯! – Melvin 2017-04-03 04:12:47

158
DELETE 
FROM m_productprice B 
    USING m_product C 
WHERE B.m_product_id = C.m_product_id AND 
     C.upc = '7094' AND     
     B.m_pricelist_version_id='1000020'; 

DELETE 
FROM m_productprice 
WHERE m_pricelist_version_id='1000020' AND 
     m_product_id IN (SELECT m_product_id 
         FROM m_product 
         WHERE upc = '7094'); 
+0

我得到同樣的錯誤! – dude 2012-08-01 06:48:54

+0

@ 0mesh它的MySQL ..我懷疑是爲SQL和Postgre sql – dude 2012-08-01 06:51:25

+0

更新我的帖子希望你有你的答案。對於較大的表格, – Omesh 2012-08-01 07:03:25

16

另一種形式,與Postgres的9.1+的工作原理是結合了公共表表達式與聯接的使用說明。

WITH prod AS (select m_product_id, upc from m_product where upc='7094') 
DELETE FROM m_productprice B 
USING prod C 
WHERE B.m_product_id = C.m_product_id 
AND B.m_pricelist_version_id = '1000020'; 
9

只要使用與INNER JOIN,LEFT JOIN或不服別人的子查詢:

DELETE FROM m_productprice 
WHERE m_product_id IN 
(
    SELECT B.m_product_id 
    FROM m_productprice B 
    INNER JOIN m_product C 
    ON B.m_product_id = C.m_product_id 
    WHERE C.upc = '7094' 
    AND B.m_pricelist_version_id = '1000020' 
) 

優化查詢,