INSERT … ON DUPLICATE KEY UPDATE with WHERE?

后端 未结 6 2085
慢半拍i
慢半拍i 2020-11-29 01:26

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.

6条回答
  •  暖寄归人
    2020-11-29 01:56

    I suggest you to use IF() to do that.

    Refer: 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);
    

提交回复
热议问题