Delete using left outer join in Postgres

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

    Instead of

    DELETE ed
    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;
    

    please try

    DELETE FROM tv_episodes
    WHERE cd_name = 'MediaLibraryDrive' AND 
      (tv_episodes.filename, tv_episodes.path IN
        (SELECT ed.filename, 
        ed.path 
        FROM tv_episodes AS ed 
        INNER JOIN data AS nd 
          ON ed.file_name = nd.file_name 
            AND ed.path = nd.path
        WHERE nd.cd_name IS NULL)
      )
      ;
    

    JOIN is not valid in a DELETE query according to the postgresql documentation. You might need to concatenate the left and right parts of the IN expression.

提交回复
热议问题