2012-08-01 20 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' 

Ich bin den folgenden Fehler bekommen PostgreSQL 8.2.11PostgreSQL löschen mit INNER JOIN

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

Ich habe versucht zu geben

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

Ich habe versucht zu geben

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

was das Problem mit meiner Abfrage?

+2

8.2? Sie sollten so schnell wie möglich upgraden. Diese Version wird nicht mehr unterstützt. Und bitte lesen Sie das Handbuch: Es gibt kein 'INNER JOIN' für die DELETE-Anweisung: http://www.postgresql.org/docs/8.2/static/sql-delete.html –

+0

Jede alternative Methode zum Ausführen dieser Abfrage ohne innere beitreten – dude

+0

Siehe das Handbuch, es gibt ein Beispiel für genau das. –

Antwort

37

Das ist für mich gearbeitet:

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

Das hat auch für mich funktioniert. Prost! – Melvin

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'; 

oder

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

ich bekomme den gleichen Fehler !!! – dude

+0

@ 0mesh ist für mysql .. mein Zweifel ist für SQL und postgre sql – dude

+0

mein Beitrag aktualisiert hoffe du hast deine Antwort. – Omesh

16

Eine andere Form, die mit Postgres arbeitet 9.1+ wird für die Verbindung einer Common Table Expression mit der USING-Anweisung kombiniert.

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

Verwenden Sie einfach eine Unterabfrage mit INNER JOIN, LEFT JOIN oder smth sonst:

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' 
) 

die Abfrage zu optimieren,