如何在插入多行时使用“ON DUPLICATE KEY UPDATE”增加MySql中的字段?
一行:
INSERT INTO table
(a, counter_elem)
VALUES
(1, 1)
ON DUPLICATE KEY UPDATE counter_elem = counter_elem+1;
对于多行:
INSERT INTO table
(a, counter_elem)
VALUES
(1, 1),
(2, 1)
ON DUPLICATE KEY UPDATE counter_elem = ?;
这不起作用:
counter_elem = VALUES(counter_elem)+1
答案 0 :(得分:23)
完全一样!
INSERT INTO table
(a, counter_elem)
VALUES
(1, 1),
(2, 1)
ON DUPLICATE KEY UPDATE counter_elem = counter_elem + 1;
那里没问题!