简体   繁体   English

在MySQL中将Magento属性值从一行复制到另一行

[英]Copy Magento attribute value from one row to another in mySQL

I would like to to copy the value of a magento attribute from one row to another row in the same table based on the ID but only if the target column is null. 我想基于ID将magento属性的值从同一行的一行复制到另一行,但前提是目标列为null。

I have a value attribute_id=99 which is the price eg 19.99, I want to copy this value to a new attribute (which is the attribute_id=1030) my test query below works fine and updates the row for the entity_id = 1030 (which is each products ID) I do not wish to manually change that figure I'd like the query to update all products that don't have a value in attribute_id = 1013 with the value from attribute_id = 99 我有一个值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;

In effect I want the entity_id = xxxx to be matched as it works though the table updating all the items. 实际上,尽管表更新了所有项目,但我希望entity_id = xxxx与之匹配,因为它可以工作。 eg 1030, 1031, 1032 ... 1099 ... 15001 例如1030、1031、1032 ... 1099 ... 15001

I'm assuming a couple of things here. 我在这里假设一些事情。 If they differ, and cause my solution to not work, please advise me through the comments: 如果它们不同,并且导致我的解决方案无法正常工作,请通过评论为我提供建议:

  1. catalog_product_entity_decimal is structured as such: catalog_product_entity_decimal的结构如下:

    value_id (autoincrement, primary key), entity_type_id, attribute_id, entity_id, value value_id(自动增量,主键),entity_type_id,attribute_id,entity_id,值

  2. There is a unique constraint on (attribute_id, entity_id). 对(attribute_id,entity_id)有唯一的约束。 ie the same attribute_id, entity_id pair can exist only once. 即,相同的attribute_id,entity_id对只能存在一次。

What you want then is an insert ignore statement: 然后,您需要的是一条插入忽略语句:

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

Where :new_attribute_id == 1030 and :attribute_id == 99, and entity_id refers to whatever product entity you want to create the new attribute for. 其中:new_attribute_id == 1030和:attribute_id == 99,entity_id指您要为其创建新属性的任何产品实体。

Also, (this is out of scope for the question) I would advise that you update the "updated_at" column in catalog_product_entity for all products for which you'll add the attribute, because if you were doing the update process through magento's orm, it would automatically note at what time the catalog_product instance was updated. 另外,(这不在问题范围内)我建议您为要为其添加属性的所有产品更新catalog_product_entity中的“ updated_at”列,因为如果您通过magento的orm进行更新,会自动记录下catalog_product实例的更新时间。

You could do this in a very simple manner. 您可以以非常简单的方式执行此操作。

Create a Procedure that: 创建一个过程,该过程:

  1. Dump the catalog_product_entity_decimal table filtered on attribute_id = 1030 into a temporary table ( temp1 ) 将按attribute_id = 1030过滤的catalog_product_entity_decimal表转储到临时表( temp1 )中

  2. Run the insert query presented in this answer 运行此答案中显示的插入查询

  3. Dump the catalog_product_entity_decimal table to a 2nd temporary table ( temp2 ) 将catalog_product_entity_decimal表转储到第二个临时表( temp2

    UPDATE catalog_product_entity p JOIN temp2 on temp2.entity_id = p.entity_id LEFT JOIN temp1 on temp1.entity_id = p.entity_id SET p.updated_at = NOW() where temp1.entity_id is null UPDATE catalog_product_entity p temp2上的JOIN temp2.​​entity_id = p.entity_id temp1.entity_id上的左联接temp1 SET p.updated_at = NOW()其中temp1.entity_id为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM