有没有办法使用 ON DUPLICATE KEY 来更新我想要插入的所有内容?

2022-01-09 00:00:00 insert mysql on-duplicate-key

我知道你可以使用 ON DUPLICATE KEY UPDATE 来更新某个值,如果该键已经有记录的话,

I know that you can use ON DUPLICATE KEY UPDATE to update a certain value if there is a record for that key already,

我可以这样做:

INSERT INTO `tableName` (`a`,`b`,`c`) VALUES (1, 2, 3)
ON DUPLICATE KEY UPDATE `a`=1, `b`=2, `c`=3

但是我怎样才能做到这一点而不必写出两次列和值呢?

But how can I do this without having to write out the columns and values twice?

推荐答案

不幸的是不是.

不必重复该值,您可以达到一半:

You can get half-way there by not having to repeat the value:

INSERT INTO `tableName` (`a`,`b`,`c`) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE `a`=VALUES(`a`), `b`=VALUES(`b`), `c`=VALUES(`c`);

但您仍然需要列出列.

相关文章