关于重复密钥更新 - MariaDB

Mus*_*afa 2 mysql sql mariadb mariasql

我有一个 MySQL 语句,一次将数据插入 4 行。该insert是工作,但我遇到的困难ON DUPLICATE KEY UPDATE

我收到一个错误: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''count = VALUES(11, 22, 33, 44)'' at line 15

下面是一个例子:

INSERT INTO table1 (id, dept, date, count)
VALUES
(1, 4, 2018-01-15, 3),
(2, 3, 2018-01-15, 4),
(3, 3, 2018-01-15, 14),
(4, 2, 2018-01-15, 11)
ON DUPLICATE KEY UPDATE
count = VALUES(11, 22, 33, 44)
Run Code Online (Sandbox Code Playgroud)

我试图包装部门和计数更新,''但这没有帮助。有没有更好的方法来更新countDUPLICATES. 能否请你帮忙!谢谢!

Bar*_*mar 6

参数 toVALUES()应该是要插入的列的名称。如果没有重复,它将使用将插入到该列中的值。

INSERT INTO table1 (id, dept, date, count)
VALUES
(1, 4, 2018-01-15, 3),
(2, 3, 2018-01-15, 4),
(3, 3, 2018-01-15, 14),
(4, 2, 2018-01-15, 11)
ON DUPLICATE KEY UPDATE
count = VALUES(count)
Run Code Online (Sandbox Code Playgroud)

如果id = 1已经存在,这会将其计数设置为3并保持所有其他列不变。