Search code examples
mysqlsqlsql-deletecascadecascading-deletes

What's wrong with the query?


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?


Solution

  • 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'