删除多个连接

时间:2015-05-29 11:11:15

标签: mysql

在我的other question中,我询问了如何从具有内部联接的多个表中选择列。我的新问题是:如何删除这些结果?

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 个答案:

答案 0 :(得分:2)

如果您只想从产品中删除,则以下内容应该完成工作

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"

但是如果您需要从匹配加入条件的所有表中删除,请使用

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"

答案 1 :(得分:1)

您可以使用此查询

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")   

<击> 编辑:来自manual

  

目前,您无法从表中删除并从中进行选择   子查询中的表。

如果要修改同一个查询,可以通过创建临时表(此处为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 )

或者你可以在MySQL手册的例子中使用这样的USING13.2.2 DELETE Syntax。我还没有使用USING,但您可以定义结帐。

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

此SO帖子也有帮助MySQL Error 1093 - Can't specify target table for update in FROM clause