开发者

How to increment a field in MySql using "ON DUPLICATE KEY UPDATE" when inserting multiple rows?

How to increment a field 开发者_C百科in MySql using "ON DUPLICATE KEY UPDATE" when inserting multiple rows?

For one row:

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

For multiple rows:

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

This doesn't work:

counter_elem = VALUES(counter_elem)+1


Exactly the same way!

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

There's no problem there!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜