gpt4 book ai didi

mysql - 使用新值更新 DUPLICATE KEY UPDATE

转载 作者:行者123 更新时间:2023-11-29 04:22:48 25 4
gpt4 key购买 nike

我有一个简单的 onupdate 表,其中包含两列:ID 和 Content 以及一条记录:[1, 'old content']。我想在添加记录时用新内容替换内容[1,'新内容']。是否可以使用 ON DUPLICATE KEY UPDATE 语法?通过这种方式,旧值保持不变:

INSERT INTO onupdate (id, content) VALUES (1, 'newcontnet')
ON DUPLICATE KEY UPDATE
onupdate.content = onupdate.content

示例仅供引用。请不要提出更新查询,因为我不知道 ID 是否存在。

最佳答案

ON DUPLICATE KEY UPDATE onupdate.content = onupdate.content 就像说“如果 id 已经存在,获取 content 并替换它本身”。您可能想将新数据放在原位:

INSERT INTO onupdate (id, content) VALUES (1, 'newcontnet')
ON DUPLICATE KEY UPDATE
onupdate.content = 'newcontnet'

关于mysql - 使用新值更新 DUPLICATE KEY UPDATE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18923747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com