如何根据旧表中的3列更新和新表?

时间:2014-04-14 13:23:27

标签: php mysql

我有两张桌子:

  1. new_product包含列:

    id_product, 
    id_category, 
    id_combination, 
    id_feature, 
    whole_sale_price, 
    retail_price,
    
  2. old_product包含列:

    id_product, 
    id_category, 
    id_combination, 
    id_feature, 
    whole_sale_price, 
    retail_price,
    
  3. 如何使用来自{{1}的whole_sale_priceretail_price列的值更新new_product上的whole_sale_priceretail_price列中的数据使用密钥old_productid_categoryid_combination

2 个答案:

答案 0 :(得分:0)

一般来说,您可以按如下方式编写:

UPDATE
  table1 AS target,
  (SELECT column1, column2 FROM table2) AS source
SET
  target.column3 = source.column1
WHERE
  target.column4 = source.column2

答案 1 :(得分:0)

如果您想更新whole_sale_priceretail_price,可以使用此查询:

UPDATE new_product as newP
JOIN old_product as oldP
ON newP.id_category = oldP.id_category AND
   newP.id_combination = oldP.id_combination AND
   newP.id_feature = oldP.id_feature
SET newP.whole_sale_price = oldP.whole_sale_price,
newP.retail_price = oldP.retail_price