Delete using left outer join in Postgres

前端 未结 4 1210
轮回少年
轮回少年 2021-02-06 22:50

I am switching a database from MySQL to Postgres SQL. A select query that worked in MySQL works in Postgres but a similar delete query does not.

I have two tables of da

4条回答
  •  日久生厌
    2021-02-06 23:34

    As others have noted, you can't LEFT JOIN directly in a DELETE statement. You can, however, self join on a primary key to the target table with a USING statement, then left join against that self-joined table.

    DELETE FROM tv_episodes
    USING tv_episodes AS ed
    LEFT OUTER JOIN data AS nd ON
       ed.file_name = nd.file_name AND 
       ed.path = nd.path
    WHERE
       tv_episodes.id = ed.id AND
       ed.cd_name = 'MediaLibraryDrive' AND nd.cd_name IS NULL;
    

    Note the self join on tv_episodes.id in the WHERE clause. This avoids the sub-query route provided above.

提交回复
热议问题