2012-07-26 101 views
2

查询有什么问题?查询有什么问题?

delete from categories c 
left join categories_products cp on cp.category_id = c.id 
left join products p on p.id = cp.product_id 
left join images i on i.object_id = cp.product_id 
where c.id = 3 and i.site_section = 'products' 

MySQL返回错误。我试图通过HeidiSQL来执行这个查询。错误未知。

另一个问题,这也将帮助我:如果我没有索引,如何才能级联删除行?

回答

4

你应该delete关键字

DELETE c FROM categories c 
      LEFT JOIN categories_products cp 
        on cp.category_id = c.id 
      LEFT JOIN products p 
        on p.id = cp.product_id 
      LEFT JOIN images i on i.object_id = cp.product_id 
WHERE c.id = 3 and i.site_section = 'products' 
+0

感谢之后添加的别名。但我试图删除'c','cp'和'i'表中的行,并且只从'c'删除行。 – dearmisterrobot 2012-07-26 12:16:21

+2

此链接可能会帮助你:) http://stackoverflow.com/questions/734567/delete-rows-from-multiple-tables – 2012-07-26 12:19:46

+0

谢谢,这个想法很清楚。这是DELETE关键字后的许多别名:) – dearmisterrobot 2012-07-26 12:21:24

1
delete c from categories c 
left join categories_products cp on cp.category_id = c.id 
left join products p on p.id = cp.product_id 
left join images i on i.object_id = cp.product_id 
where c.id = 3 and i.site_section = 'products' 

加入时,必须指定从哪个表中删除。这就是为什么它是delete c from ...