INSERT ... ON DUPLICATE KEY UPDATE with WHERE?(INSERT ... ON DUPLICATE KEY UPDATE with WHERE?)
问题描述
我正在执行 INSERT ... ON DUPLICATE KEY UPDATE
但我需要更新部分是有条件的,只有在某些额外条件发生变化时才进行更新.
I'm doing a INSERT ... ON DUPLICATE KEY UPDATE
but I need the update part to be conditional, only doing the update if some extra condition has changed.
但是,此UPDATE
不允许使用WHERE
.有什么解决方法吗?
However, WHERE
is not allowed on this UPDATE
. Is there any workaround for this?
我不能做 INSERT/UPDATE/SELECT 的组合,因为这需要在复制上工作.
I can't do combinations of INSERT/UPDATE/SELECT since this needs to work over a replication.
推荐答案
我建议你使用 IF() 来做到这一点.
I suggest you to use IF() to do that.
参考:conditional-duplicate-key-updates-with-mysql
INSERT INTO daily_events (created_on, last_event_id, last_event_created_at)
VALUES ('2010-01-19', 23, '2010-01-19 10:23:11')
ON DUPLICATE KEY UPDATE
last_event_id = IF(last_event_created_at < VALUES(last_event_created_at), VALUES(last_event_id), last_event_id);
这篇关于INSERT ... ON DUPLICATE KEY UPDATE with WHERE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:INSERT ... ON DUPLICATE KEY UPDATE with WHERE?


- SQL 临时表问题 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01