我有一个eshop数据库,我复制了一些产品,以便将它们转移到另一个类别(批发/零售)。我需要更新每个名称中包含“[CLONE]”的产品的category_id,这就是我正在做的事情:
首先,我检查需要移动的产品数量:
select p.product_id, pd.product, pc.category_id
from cscart_products p
join `cscart_product_descriptions` pd on p.product_id = pd.product_id
join `cscart_products_categories` pc on p.product_id = pc.product_id
where pd.product like '%CLONE%'
and pc.category_id = '17'; -- 17 is the current category_id
提取了16行;
然后,我执行更新:
UPDATE cscart_products_categories pc
join `cscart_product_descriptions` pd on pc.product_id = pd.product_id
join `cscart_products` p on pc.product_id = p.product_id
SET pc.category_id = '30' -- the category to be moved to
WHERE pc.category_id = '17'
AND pd.product like '%[CLONE]%';
受影响的9行;
正如你所看到的,16个中只有9个正在更新,显然我的更新声明中有些错误但是什么?可在此处找到示例数据http://www.megaupload.com/?d=AM85UQFY
答案 0 :(得分:1)
您的第一个查询找到16行category_id
= 17。
您的第二个查询更改了9行HAD category_id 30 to now have
category_id 17.
因此,在第二个查询之后,重新运行第一个查询应该发现带有`category_id 17的16 + 9 = 25条记录。
如果您想要发现将更新多少结果,您应该使用`category_id = 30运行第一个查询:
select p.product_id, pd.product, pc.category_id
from cscart_products p
join `cscart_product_descriptions` pd on p.product_id = pd.product_id
join `cscart_products_categories` pc on p.product_id = pc.product_id
where pd.product like '%CLONE%'
and pc.category_id = '30'; -- the one that will be changed
- 找到9行
UPDATE cscart_products_categories pc
join `cscart_product_descriptions` pd on pc.product_id = pd.product_id
join `cscart_products` p on pc.product_id = p.product_id
SET pc.category_id = '17'
WHERE pc.category_id = '30' -- the category to be moved to
AND pd.product like '%[CLONE]%';
- 影响了9行