我正在执行 INSERT ... ON DUPLICATE KEY UPDATE
但我需要更新部分是有条件的,只有在某些额外条件发生变化时才进行更新。
但是,WHERE
在此 UPDATE
上是不允许的。有什么解决方法吗?
我不能进行 INSERT/UPDATE/SELECT 的组合,因为这需要在复制过程中起作用。
最佳答案
我建议你使用 IF() 来做到这一点。
引用: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);
https://stackoverflow.com/questions/2469457/
相关文章:
mysql - 如何让 MySQL 数据库完全在内存中运行?
java - 使用 Java 通过 SSH 连接到远程 MySQL 数据库
mysql - 带有引用 MySQL 中同一个表的子查询的 SQL UPDATE
php - 选择在 wp_usermeta 表中的多行上具有合格数据的用户
python - 如何安装 MySQLdb 包? (导入错误 : No module named s
sql - BIGINT(8) 是 MySQL 可以存储的最大整数吗?
mysql - org.hibernate.AssertionFailure : null id i