0

I'm trying to delete all records form the table "oc_products" that don't have a certain category ID. I created a SELECT query that lists those products using an INNER JOIN, since the categories are in a separate table.

What I can't figure out is how to use the DELETE function to delete the shown records.

This is what my code looks like:

DELETE oc_product
FROM oc_product
INNER JOIN oc_product_to_category ON oc_product.product_id = oc_product_to_category.product_id
WHERE oc_product_to_category.category_id = 343

Its showing the error "Unexpected keyword, (near INNER JOIN)".

2
  • write a sub query to get all records and then use where clause. DELETE oc_product FROM oc_product where oc_product in (sub query ) Commented Dec 11, 2017 at 22:15
  • @sreenivas I tried it but the error "Table 'oc_product' is specified twice, both as a target for 'DELETE' and as a separate source for data" comes up. I understand the problem but what do I need to change? Commented Dec 11, 2017 at 23:55

1 Answer 1

1

Add .* to p in your first line.

Try:

DELETE p.* FROM oc_product p
INNER JOIN oc_product_to_category pc ON p.product_id = 
    pc.product_id
WHERE pc.category_id = 343
Sign up to request clarification or add additional context in comments.

2 Comments

It works perfectly. Could you perhaps explain what the " .* " does exactly, and what the "p" and "pc" stand for? Thanks a lot for the help.
p and pc are aliases so that you don't have to use the full table name every time. p.* just says that delete rows from p and not from pc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.