0

I have a query like this :

DELETE dcp 
FROM discount_campaign_product dcp 
JOIN product_categories pc ON dcp.product_id = pc.product_id 
JOIN categories c ON pc.category_id = c.id 
WHERE dcp.discount_campaign_id = discount_campaign_id AND
    FIND_IN_SET(c.id, delete_category) AND 
    EXISTS(SELECT 1 
           FROM discount_campaign_product dcp2 
           WHERE dcp.product_id = dcp2.product_id AND 
                 dcp2.discount_campaign_id = discount_campaign_id);

but I get this error

Error Code: 1093. You can't specify target table 'dcp' for update in FROM clause

I tried with this solution https://stackoverflow.com/a/45498/12977554

with this query SET optimizer_switch = 'derived_merge=off';

but still produce same error

Giorgos Betsos
  • 71,379
  • 9
  • 63
  • 98
18818181881
  • 105
  • 7

1 Answers1

1

Try this:

DELETE dcp 
FROM discount_campaign_product dcp 
JOIN product_categories pc ON dcp.product_id = pc.product_id 
JOIN categories c ON pc.category_id = c.id 
WHERE dcp.discount_campaign_id = discount_campaign_id AND
    FIND_IN_SET(c.id, delete_category) AND
    product_id IN (SELECT product_id
                   FROM (
                      SELECT product_id
                      FROM discount_campaign_product
                      GROUP BY product_id
                      HAVING COUNT(*) > 1) t)
Giorgos Betsos
  • 71,379
  • 9
  • 63
  • 98