我有一个sql语句,如下所示:

delete from survey_results sr
left outer join people p on p.id = sr.person_id
left outer join survey_result_sets srs on srs.id = sr.survey_result_set_id
left outer join survey_schemas ss on ss.id = sr.survey_schema_id
where (sr.person_id is not null and p.id is null)
OR (srs.id is null)
OR (ss.id is null);

但它有一个语法错误:
ERROR:  syntax error at or near "left"
LINE 2: left outer join people p on p.id = sr.person_id

为什么?

最佳答案

在PostgreSQL中,不能在DELETE语句中使用JOIN。相反,使用并将第二个表放在那里。像这样的事情应该行得通

   delete from survey_results sr
using people p on p.id = sr.person_id
using  survey_result_sets srs on srs.id = sr.survey_result_set_id
using  survey_schemas ss on ss.id = sr.survey_schema_id
where (sr.person_id is not null and p.id is null)
OR (srs.id is null)
OR (ss.id is null);

你可以参考这个PostgreSQL-delete

关于sql - sql语句中的语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30044863/

10-11 01:43