【问题标题】:Copy Magento attribute value from one row to another in mySQL在 mySQL 中将 Magento 属性值从一行复制到另一行
【发布时间】:2014-07-21 20:41:22
【问题描述】:

我想根据 ID 将 magento 属性的值从一行复制到同一表中的另一行,但前提是目标列为空。

我有一个值 attribute_id=99,即价格,例如 19.99,我想将此值复制到一个新属性(即 attribute_id=1030)我下面的测试查询工作正常并更新 entity_id = 1030 的行(这是每个产品 ID)我不希望手动更改该数字我希望查询更新所有在 attribute_id = 1013 中没有值的产品,其值来自 attribute_id = 99

UPDATE catalog_product_entity_decimal 
SET value = (select value from (select value from catalog_product_entity_decimal where attribute_id = 99 AND entity_id = 1030) AS x)
WHERE attribute_id = 1013 AND attribute_id IS NULL AND entity_id = 1030;

实际上,我希望 entity_id = xxxx 匹配,因为它可以通过表格更新所有项目来工作。例如1030, 1031, 1032 ... 1099 ... 15001

【问题讨论】:

    标签: mysql sql magento


    【解决方案1】:

    我在这里假设了几件事。如果它们不同,并导致我的解决方案不起作用,请通过 cmets 告诉我:

    1. catalog_product_entity_decimal 的结构如下:

      value_id(自增,主键),entity_type_id,attribute_id,entity_id,value

    2. (attribute_id, entity_id) 存在唯一约束。即相同的attribute_id,entity_id对只能存在一次。

    那么你想要的是一个插入忽略语句:

    INSERT IGNORE INTO catalog_product_entity_decimal (entity_id, attribute_id, value)
    SELECT entity_id, :new_attribute_id as `attribute_id`, value
    FROM catalog_product_entity_decimal
    WHERE entity_id = :entity_id
    AND attribute_id = :attibute_id
    

    其中 :new_attribute_id == 1030 和 :attribute_id == 99,而 entity_id 是指您要为其创建新属性的任何产品实体。

    另外,(这超出了问题的范围)我建议您更新 catalog_product_entity 中的“updated_at”列,以便为您添加属性的所有产品,因为如果您通过 magento 进行更新过程orm,它会自动记录 catalog_product 实例的更新时间。

    您可以通过非常简单的方式完成此操作。

    创建一个过程:

    1. 将根据 attribute_id = 1030 过滤的 catalog_product_entity_decimal 表转储到临时表 (temp1)

    2. 运行此答案中提供的插入查询

    3. 将 catalog_product_entity_decimal 表转储到第二个临时表 (temp2)

      更新目录产品实体 p 在 temp2.​​entity_id = p.entity_id 上加入 temp2 在 temp1.entity_id = p.entity_id 上左连接 temp1 SET p.updated_at = NOW() 其中 temp1.entity_id 为空

    【讨论】:

      猜你喜欢
      • 2011-06-12
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2012-06-09
      • 2021-12-12
      • 1970-01-01
      • 2018-10-31
      相关资源
      最近更新 更多