用其他列中的值更新列

时间:2018-08-15 16:09:24

标签: mysql magento mysqli magento-1.9 magento2

我想将special_price更新为magento 2中的现有价格。我该怎么做?

price和special_price属性都存储在表catalog_product_entity_decimal中,其属性代码为74和75。


catalog_product_entity_decimal table


现在,我必须将1349(entry_id = 9773和attribute_id = 75的值)更新为1619(entry_id = 9773和attribute_id = 74的值)

类似地,要将entity_id = 9774和attribute_id = 75的值从1349更新为1619。

每个实体的每个attribute_id都有不同的值。

我需要sql update语句,它可以一次更新表中这种200,000个实体的值。此sql将在一个sql语句中将magento 2中的所有产品的价格和special_price彼此更新。

我尝试运行此sql,但是没有用:

UPDATE catalog_product_entity_decimal val
SET val.value = (SELECT value FROM catalog_product_entity_decimal WHERE attribute_id = '74' and entity_id='9773')
WHERE val.attribute_id = '75' and val.entity_id='9773'

错误提示“选择不能来自同一表”

1 个答案:

答案 0 :(得分:0)

您可以尝试使用内部联接

  UPDATE catalog_product_entity_decimal val
  INNER JOIN catalog_product_entity_decimal val2 ON val.attribute_id = '75' 
        and val.entity_id ='9773'
          and val2.attribute_id = '74' and val2.entity_id='9773'
  SET val.value = val2.value 

或基于子选择的内部联接

  UPDATE catalog_product_entity_decimal val
  INNER JOIN ( select  value, attribute_id, entity_id 
    from  catalog_product_entity_decimal val2
    where  val21.attribute_id = '74' and val2.entity_id='9773'
  ) t ON val.attribute_id = '75' 
        and val.entity_id ='9773'
  SET val.value = t.value 

对于所有您必须在联接条件中使用此列的entity_id

  UPDATE catalog_product_entity_decimal val
  INNER JOIN catalog_product_entity_decimal val2 ON val.attribute_id = '75' 
          and val2.attribute_id = '74' 
            and val2.attribute_id = val.attribute_id 
  SET val.value = val2.value 

OR

 UPDATE catalog_product_entity_decimal val
  INNER JOIN ( select  value, attribute_id, entity_id 
    from  catalog_product_entity_decimal val2
    where  val21.attribute_id = '74' 
  ) t ON val.attribute_id = '75' 
        and val2.attribute_id = t.attribute_id 
  SET val.value = t.value