0
INSERT INTO min_product ( shopid, productid , outward ) 
SELECT shopid, productid , outward 
FROM main_product 
WHERE shopid= '51' 
    ON DUPLICATE KEY UPDATE outward  = ( SELECT sum(outward)  
                                         FROM main_product WHERE shopid= '51' )

main_product 表

productid outward shopid
    333      2      44//present
    343      4      44//present
    353      5      44//present
    363      1      44//present
    373      2      44//not present

min_product 表

productid outward shopid

333      1      44
343      1      44
353      1      44
363      1      44

问题是它不会更新重复行而是插入新行?查询中有什么问题ON DUPLICATE KEY UPDATE

4

1 回答 1

0

如果您的数据库允许插入重复行,则您的表定义不正确 - 仅在插入 UNIQUE 索引或 PRIMARY KEY 的副本时才执行更新

INSERT ... ON DUPLICATE KEY UPDATE 语法

于 2012-11-10T11:42:18.680 回答