2

What's wrong with the query?

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 returns error. I'm trying to execute this query through the HeidiSQL. Error is unknown.

Another question, which will also help me: how can I make cascade deletion of row if I have no indexes?

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
neochar
  • 164
  • 1
  • 5
  • 16

2 Answers2

4

you should add the alias after the delete keyword

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'
John Woo
  • 249,283
  • 65
  • 481
  • 481
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'

When joining you have to specify from what table you are deleting. That is why it is delete c from ...

juergen d
  • 195,137
  • 36
  • 275
  • 343