Delete using left outer join in Postgres

前端 未结 4 1254
轮回少年
轮回少年 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:45

    As bf2020 points out, postgres does not support JOINs when conducting a DELETE query. The proposed solution of a sub-query made me think of the solution. Refine the SELECT query from above and employ it as a sub-query to a DELETE query statement:

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

    Sub-queries can often be inefficient consuming time and CPU resources with some database systems, especially MySQL. From my experience I try to avoid using a sub-query due to that inefficiency plus that such queries are sometimes an easy way out to honing one's skill like learning JOIN syntax.

    Since postgre does not permit delete queries using join, the above is the solution that works.

提交回复
热议问题