如何使用多个jeft连接编写MySQL删除

时间:2015-10-01 21:25:00

标签: mysql left-join sql-delete

我正在使用MySQL 5.5.37。我想写一个DELETE语句,我说从表中删除" c"如果c在表A或表B中没有匹配的行。所以我尝试了这个

ERROR 1064 (42000): You have an error in your SQL syntax; check 
the manual that corresponds to your MySQL server version for 
the right syntax to use near 'c left join sb_lesson_plan_classroom 
lpc on c.id = lpc.classroom_id left join sb' at line 1

不幸的是,上述情况并没有发生并且因错误

而死亡
{{1}}

构造delete语句以合并左连接的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

将您的DELETE查询语法更改为如下所示。由于您要声明表别名c,因此应在DELETE语句中使用它。

delete c from classroom c
left join lesson_plan_classroom lpc on c.id = lpc.classroom_id 
left join response r on c.id = r.classroom_id 
where r.classroom_id is null 
and lpc.classroom_id is null 
and c.enabled = 0;

答案 1 :(得分:0)

我已经编辑了你的帖子并纠正了看似拼写错误的内容,但也许'incoprorate'是对你当前SQL的恰当描述。

考虑:

DELETE from classroom c 
WHERE c.enabled is false
AND c.id NOT IN (
   SELECT r.classroom_id
   FROM response r
)
AND c.id NOT IN (
    SELECT lpc.classroom_id
    FROM lesson_plan_classroom lpc)