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.
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
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. eg 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:
catalog_product_entity_decimal is structured as such:
value_id (autoincrement, primary key), entity_type_id, attribute_id, entity_id, value
There is a unique constraint on (attribute_id, entity_id). ie the same attribute_id, entity_id pair can exist only once.
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.
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.
You could do this in a very simple manner.
Create a Procedure that:
Dump the catalog_product_entity_decimal table filtered on attribute_id = 1030 into a temporary table ( temp1
)
Run the insert query presented in this answer
Dump the catalog_product_entity_decimal table to a 2nd temporary table ( 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.