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
答案 0 :(得分:0)
如果您的数据库允许插入重复行,则表的定义不正确 - 仅在插入UNIQUE索引或PRIMARY KEY的副本时才执行更新