2012-07-26 168 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 ...