且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Postgres中使用左外部联接删除

更新时间:2023-11-22 11:53:34

正如其他人指出的那样,您不能直接在DELETE语句中左移JOIN。但是,您可以使用USING语句在主键上自动联接到目标表,然后对该联接表进行左联接。

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;

请注意WHERE子句中tv_episodes.id上的自连接。这样可以避免上面提供的子查询路由。

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