基于其他2个值从mysql表中删除

时间:2017-10-18 09:06:46

标签: mysql sql

我试图从rk21a_virtuemart_product_categories = 251和virtuemart_category_id来自product_discount_id = 0

rk21a_virtuemart_product_prices表中删除

尝试过此查询但不起作用(删除virtuemart_category_id = 251而不是两个条件的所有内容)

DELETE FROM rk21a_virtuemart_product_categories
   WHERE virtuemart_category_id = 251
   AND virtuemart_product_id IN (
   SELECT virtuemart_product_id
   FROM rk21a_virtuemart_product_prices
   WHERE product_discount_id = 0)

表格是:

**rk21a_virtuemart_product_categories**
id   virtuemart_product_id  virtuemart_category_id
12   119                    160
13   121                    251
15   122                    173
20   123                    251

**rk21a_virtuemart_product_prices**
virtuemart_product_price_id virtuemart_product_id product_discount_id
1021                         119                  5
1022                         121                  0
1023                         122                  0
1024                         123                  10

有人可以告诉我我做错了什么吗?我是初学者.. 谢谢,

1 个答案:

答案 0 :(得分:0)

请改为尝试:

DELETE c
FROM rk21a_virtuemart_product_categories AS c
INNER JOIN rk21a_virtuemart_product_prices AS p 
        ON c.virtuemart_product_id  = p.virtuemart_product_id
WHERE c.virtuemart_category_id = 251
  AND p.product_discount_id = 0;

demo

在第一个表rk21a_virtuemart_product_categories中有两行,类别为id = 251,但在第二个表中只有一行.product_discount_id = 0,因此只删除一行。< / p>