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'
je reçois l'erreur suivante PostgreSQL 8.2.11
ERROR: syntax error at or near "B"
LINE 1: DELETE B.* from m_productprice B INNER JOIN m_product C ON ...
j'ai essayé de donner
DELETE B from m_productprice B INNER JOIN m_product C ON B....
ERROR: syntax error at or near "B"
j'ai essayé de donner
ERROR: syntax error at or near "INNER"
LINE 1: DELETE from m_productprice B INNER JOIN m_product C ON B.m_...
quel est le problème avec ma requête?
Cela a fonctionné pour moi:
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');
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';
ou
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');
Un autre formulaire qui fonctionne avec Postgres 9.1+ combine une expression de table commune avec l’instruction USING pour la jointure.
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';
Utilisez simplement une sous-requête avec INNER JOIN, LEFT JOIN ou autre:
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'
)
pour optimiser la requête,
IN
Si vous avez plus d'une jointure, vous pouvez utiliser des instructions USING séparées par des virgules:
DELETE
FROM
AAA AS a
USING
BBB AS b,
CCC AS c
WHERE
a.id = b.id
AND a.id = c.id
AND a.uid = 12345
AND c.gid = 's434sd4'