[sql] 내부 조인으로 PostgreSQL 삭제

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.11 오류가 발생합니다

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_...

내 쿼리의 문제점은 무엇입니까?



답변

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


답변

이것은 나를 위해 일했다 :

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


답변

Postgres 9.1+에서 작동하는 다른 형식은 공통 테이블 표현식과 조인에 대한 USING 문을 결합하는 것입니다.

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


답변

하나 이상의 조인이있는 경우 쉼표로 구분 된 USING 문을 사용할 수 있습니다.

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'

참고


답변

INNER JOIN, LEFT JOIN 또는 smth와 함께 하위 쿼리를 사용하십시오.

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

검색어를 최적화하기 위해


답변