2014-07-21 22 views
0

這是我的第一個問題,請隨身攜帶。在mySQL中將Magento屬性值從一行復制到另一行

我想複製一個magento屬性的值從一行到同一個表中的另一行基於ID,但只有當目標列爲空。

我有一個值attribute_id = 99這是價格,例如19.99,我想複製這個值到一個新的屬性(這是attribute_id = 1030)我的測試查詢下面工作正常,更新行的entity_id = 1030(這是每個產品ID)我不希望手動更改該數字我希望查詢更新屬性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

非常感謝

史蒂夫

回答

0

我在這裏假設一對夫婦的事情。如果它們不同,而導致我的解決方案到不行,請告知我通過評論:

  1. catalog_product_entity_decimal的結構爲這樣:

    value_id(自動增量,主鍵),entity_type_id,attribute_id,ENTITY_ID,值

  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

    UPDATE catalog_product_entity p 在temp2上加入temp2。ENTITY_ID = p.entity_id LEFT JOIN上temp1.entity_id = p.entity_id SET p.updated_at = temp1中NOW() 其中temp1.entity_id爲空

相關問題