设置旧值=旧值+新值ON DUPLICATE KEY

时间:2019-04-13 12:34:49

标签: mysql

我看到了这个示例,我想用旧值+新值更新旧值

INSERT INTO mytable (id, a, b, c)
VALUES (1, 'a1', 'b1', 'c1'),
(2, 'a2', 'b2', 'c2'),
(3, 'a3', 'b3', 'c3'),
(4, 'a4', 'b4', 'c4'),
(5, 'a5', 'b5', 'c5'),
(6, 'a6', 'b6', 'c6')
ON DUPLICATE KEY UPDATE id=VALUES(id),
a=VALUES(old a+ new a),
b=VALUES(old b+ new b),
c=VALUES(old c+ new c);

1 个答案:

答案 0 :(得分:0)

感谢我已经解决了我的问题

INSERT INTO mytable (id, a, b, c)
VALUES (1, 'a1', 'b1', 'c1'),
(2, '5', '6', '7'),
(3, '5', '6', '7'),
(4, '5', '6', '7'),
(5, '5', '6', '7'),
(6, '5', '6', '7')
ON DUPLICATE KEY UPDATE id=VALUES(id),
a=a+VALUES(a),
b=b+VALUES(b),
c=c+VALUES(c);