带连接的SQL删除导致语法错误

时间:2014-06-04 21:49:30

标签: mysql codeigniter

我正在尝试使用sql删除子表数据,但我收到的错误

  

"您的SQL语法中出现错误:' ON t1.foodUnit_relation_Id = t2.food_unit_relation_id WHERE   t1.id = 7'第3行"

这是我的疑问:

   DELETE t2.* 
   FROM food_details t1, food_units_relation t2 
   ON t1.foodUnit_relation_Id = t2.food_unit_relation_id 
   WHERE t1.id =7

其中:

  • food_details是父表& food_units_relation是一个子表。

  • food_unit_relation_id的{​​{1}}列 引用food_details

  • 的字段foodUnit_relation_Id

任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

您忘记了JOIN关键字。

你可以在没有加入的情况下完成:

DELETE FROM food_units_relation
WHERE food_unit_relation_id IN (SELECT foodUnit_relation_Id FROM food_details WHERE id = 7);

或者加入JOIN:

DELETE food_units_relation
FROM food_units_relation
JOIN food_details ON food_details.foodUnit_relation_Id = food_units_relation.food_unit_relation_id 
WHERE food_details.id =7