在重复密钥更新 - MariaDB

时间:2018-01-15 22:52:25

标签: 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)

我试图用''包裹部门和计数更新,但这没有帮助。有没有更好的方法来更新重复count上的重复项。能否请你帮忙!谢谢!

1 个答案:

答案 0 :(得分:4)

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

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)

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

相关问题