我有一个更新 Magento 商店 priselist 的项目。我有要更新的项目列表,我想用一个 MySQL 查询来完成(通过思考,它更有效率)。 priselist 包括 sku 和价格。
我有这两张表。从表 1 我使用 sku 更新表 2/strong> value,这是当 *entity_id* 在两个表中相等且 attribute_id 为“64”时商品的价格。
表 1
product_entity
entity_id | sku
1 | p1
2 | p2
3 | p3
表2
product_entity_decimal
entity_id | attribute_id | value |
1 | 64 | 5 |
1 | 65 | NULL |
1 | 66 | NULL |
2 | 64 | 7 |
2 | 65 | NULL |
2 | 66 | NULL |
3 | 64 | 1 |
3 | 65 | NULL |
3 | 66 | NULL |
那么,我如何更新一个查询 p1 奖品为 6 和 p3 奖品为 2。
我尝试了这个查询,但没有成功...
UPDATE product_entity, product_entity_decimal SET product_entity_decimal.value =
CASE
WHEN product_entity.entity_id = product_entity_decimal.entity_id AND product_entity_decimal.attribute_id = '64' AND product_entity.sku = 'p1' THEN '6'
WHEN product_entity.entity_id = product_entity_decimal.entity_id AND product_entity_decimal.attribute_id = '64' AND product_entity.sku = 'p1' THEN '2'
ELSE value
END
对于我的项目,我通过使用 MySql.Data.MySqlClient 将 C# 与 .Net 和 MySQL 5.x 结合使用。如果有请分享,也许有更好的方法来更新 MySQL 数据库中 3000 项的列表。 :)
最佳答案
您可以在 UPDATE
中使用 JOIN
:
UPDATE product_entity_decimal a
JOIN product_entity b ON a.entity_id = b.entity_id AND b.sku IN ('p1', 'p3')
SET a.value = CASE b.sku WHEN 'p1' THEN 6 WHEN 'p2' THEN 2 END
WHERE a.attribute_id = 64
如果您有一个包含 3000 个要更新的项目的列表,也许最好的方法是遍历数组并在每次迭代中执行一个 UPDATE
。这些更新将被封装在一个事务中,例如在这个伪代码中:
// START TRANSACTION
// For Each Value in array
// Execute UPDATE statement
// COMMIT
关于C# 项目更新从两个表中选择的行的 MySQL 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11814670/