on重复密钥更新-mariadb

a9wyjsp7  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(694)

我有一个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)

我试着把这个部门 Package 起来,用最新的数据统计一下 '' 但那没用。有没有更好的方法来更新 count 在复制品上。你能帮帮我吗!谢谢!

mi7gmzs6

mi7gmzs61#

对…的论点 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 其他列保持不变。

相关问题