I've got the following select query:

SELECT opt.product_option_id 
FROM  `oc_product_option_value` AS opt_val,  `oc_product_option` AS opt
WHERE  `opt`.`product_option_id` =  `opt_val`.`product_option_id` 
AND  `opt_val`.`price` =  '0.0000'

I thought that i could use that in an delete query:

DELETE oc_product_option, oc_product_option_value FROM oc_product_option 
INNER JOIN oc_product_option_value

WHERE  `oc_product_option`.`product_option_id` =  `oc_product_option_value`.`product_option_id` 
AND  `oc_product_option_value`.`price` =  '0.0000'

But that deleted ALL rows in oc_product_option. So what did i do wrong?

有帮助吗?

解决方案

Can you give this a try?

DELETE t1 FROM oc_product_option t1
JOIN oc_product_option_value t2
WHERE  `t1`.`product_option_id` =  `t2`.`product_option_id` 
AND  `t2`.`price` =  '0.0000'

Is it the intetion to delete the rows of both tables?

其他提示

You make a circular dependence and this is why you get this result

Bellow query should delete only rows that have id in oc_product_option and price zero. Or you have to define range of ids in oc_product_option using another criteria to filter oc_product_option table

DELETE FROM oc_product_option o
 WHERE  o.product_option_id in (SELECT opt.product_option_id FROM oc_product_option_value
                                 where `oc_product_option_value`.`price` =  '0.0000')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top