MySQL 将单元格从一个 table 更新为另一个
MySQL update cell from one table to anoher
我需要从 table ps_product 中获取 id_product 的值 ecotax,并更新 table ps_product 中相同 id_product 的值 ecotax _shop.
执行此命令:
UPDATE `ps_product_shop`
SET `ecotax` = (SELECT `ecotax` FROM `ps_product`
WHERE ps_product.`id_product` =
ps_product_shop.`id_product`);
但得到:
#1048 - Column 'ecotax' cannot be null
值为 0.0000 或 0.1580 等等。在 SELECT 中也尝试过 IN NOT NULL 但没有帮助。命令由 phpMyAdmin 处理。
有什么想法吗?
您可以通过以下两种方式:
第一个:
UPDATE ps_product_shop
SET ecotax =
(SELECT ps_product.ecotax FROM ps_product WHERE ps_product.id_product =
ps_product_shop.id_product)
WHERE EXISTS(SELECT 1 FROM ps_product WHERE ps_product.id_product =
ps_product_shop.id_product)
第二个:
UPDATE ps_product_shop
SET ecotax =
(SELECT ps_product.ecotax FROM ps_product WHERE ps_product.id_product =
ps_product_shop.id_product)
WHERE (SELECT ps_product.ecotax FROM ps_product WHERE ps_product.id_product =
ps_product_shop.id_product) IS NOT NULL
我能想到的解释这一点的唯一可能原因是您没有在子查询中使用适当的别名限定 ecotax
。尝试这样做:
UPDATE ps_product_shop ps
SET ecotax = (SELECT p.ecotax FROM ps_product p
WHERE p.id_product = ps.id_product);
但我实际上建议在这里利用 MySQL 的更新连接语法(假设您实际上使用的是 MySQL):
UPDATE ps_product_shop ps
LEFT JOIN ps_product p
ON p.id_product = ps.id_product
SET
ecotax = ps.ecotax;
我需要从 table ps_product 中获取 id_product 的值 ecotax,并更新 table ps_product 中相同 id_product 的值 ecotax _shop.
执行此命令:
UPDATE `ps_product_shop`
SET `ecotax` = (SELECT `ecotax` FROM `ps_product`
WHERE ps_product.`id_product` =
ps_product_shop.`id_product`);
但得到:
#1048 - Column 'ecotax' cannot be null
值为 0.0000 或 0.1580 等等。在 SELECT 中也尝试过 IN NOT NULL 但没有帮助。命令由 phpMyAdmin 处理。
有什么想法吗?
您可以通过以下两种方式:
第一个:
UPDATE ps_product_shop
SET ecotax =
(SELECT ps_product.ecotax FROM ps_product WHERE ps_product.id_product =
ps_product_shop.id_product)
WHERE EXISTS(SELECT 1 FROM ps_product WHERE ps_product.id_product =
ps_product_shop.id_product)
第二个:
UPDATE ps_product_shop
SET ecotax =
(SELECT ps_product.ecotax FROM ps_product WHERE ps_product.id_product =
ps_product_shop.id_product)
WHERE (SELECT ps_product.ecotax FROM ps_product WHERE ps_product.id_product =
ps_product_shop.id_product) IS NOT NULL
我能想到的解释这一点的唯一可能原因是您没有在子查询中使用适当的别名限定 ecotax
。尝试这样做:
UPDATE ps_product_shop ps
SET ecotax = (SELECT p.ecotax FROM ps_product p
WHERE p.id_product = ps.id_product);
但我实际上建议在这里利用 MySQL 的更新连接语法(假设您实际上使用的是 MySQL):
UPDATE ps_product_shop ps
LEFT JOIN ps_product p
ON p.id_product = ps.id_product
SET
ecotax = ps.ecotax;