使用 Join with PostgreSQL 从多个表中删除记录

Delete records from multiple tables using Join with PostgreSQL

我正在使用 PostgreSQL 11 数据库,我有三个相关的 table 外键,T1、T2 和 T3。 T1 和 T2 之间存在多对多关系,所以我使用 T3 作为连接 table。 我正在尝试使用 JOIN 通过一个查询从这三个 table 中删除记录,我试过:

DELETE t1, t3, t2 FROM T1 AS t1 
INNER JOIN T3 AS t3 ON t1.id = t3.t1_id 
INNER JOIN T2 AS t2 ON t2.id = t3.t2_id 
WHERE t1.col = 'something';

我运行这个查询使用pgAdmin,它returns:

ERROR:  ERROR: syntax error near « t1 »
LINE 1: DELETE t1, t3, t2 FROM T1 ...

我这里的查询语法有什么问题?我错过了什么吗?

相反,一次从一个 table 中删除:

with t1 as (
      delete t1 
      where t1.col = 'something'
      returning *
     ),
     t3 as (
      delete t3
      where t3.t1_id in (select id from t1)
      returning *
     )
delete t2
where t2.id in (select t2_id from t3);

不完全相同。内部联接需要 table 之间的匹配。但我认为这是你的意图。