如何在插入多行时使用“ON DUPLICATE KEY UPDATE”增加MySql中的字段?

时间:2011-07-23 17:55:44

标签: mysql sql mysql-error-1064

如何在插入多行时使用“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

1 个答案:

答案 0 :(得分:23)

完全一样!

INSERT INTO table 
  (a, counter_elem) 
VALUES 
  (1, 1),
  (2, 1)
ON DUPLICATE KEY UPDATE counter_elem = counter_elem + 1;

那里没问题!