0

In my other question I asked how to select a column from multiple tables with inner joins. My new question is: how to delete these results?

SELECT 
product_image.image 
FROM product 
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type"

2 Answers 2

2

If you want to delete only from products then the below should do the job

delete p from product p
INNER JOIN ixml_prd_map ipm ON p.sku = ipm.id_oc_prd
INNER JOIN product_image pi ON pi.product_id = p.product_id
WHERE p.model = "xy-type"

But if you need to delete from all the tables matching the joining condition then use

delete p,ipm,pi from product p
INNER JOIN ixml_prd_map ipm ON p.sku = ipm.id_oc_prd
INNER JOIN product_image pi ON pi.product_id = p.product_id
WHERE p.model = "xy-type"
Sign up to request clarification or add additional context in comments.

2 Comments

I just want to delete matched "product_image.image".
Yes in the first case the deletion will happen on product table by matching the joining clause and condition and in the 2nd case its deleting from all the tables. You may use which one is convenient for you.
1

You can use this query

DELETE FROM product_image WHERE product_image_id IN (SELECT 
product_image.image 
FROM product 
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type")   

EDIT : From the manual

Currently, you cannot delete from a table and select from the same table in a subquery.

If you want to modify the same query you can execute it by creating a temporary table (here its resultset)

DELETE FROM product_image WHERE product_image_id IN ( SELECT resultset.product_image_id FROM (SELECT 
    product_image.product_image_id
    FROM product 
    INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
    INNER JOIN product_image ON product_image.product_id = product.product_id
    WHERE product.model = "xy-type") AS resultset )

OR you can use the USING like this in the example from the MySQL Manual, 13.2.2 DELETE Syntax. I haven't used the USING, but you can definetely check out.

DELETE FROM t1, t2 USING t1 
INNER JOIN t2 
INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;    

Also this SO post will help too MySQL Error 1093 - Can't specify target table for update in FROM clause

6 Comments

For some reason I am getting error as above: #1093 - You can't specify target table 'product_image' for update in FROM clause
I believe its because In mysql, you cannot update a table and select from the same table in a subquery, Currently, you cannot delete from a table and select from the same table in a subquery. : From the manual dev.mysql.com/doc/refman/5.0/en/delete.html
Uh-oh. I would delete manually, but there are more than 30 000 result. :(
@Adrian you can delete it via query, what I meant was deletion is not possible using the older query, the updated one should work.
Oh sorry that was a mistake. I have updated the query. I mistyped image for product_image_id
|

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.