小编典典

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

sql

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

阅读 220

收藏
2021-04-17

共1个答案

小编典典

完全一样!

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

那里没有问题!

2021-04-17