

INSERT INTO last_session_user_events ( user_id, last_session_id, last_session_created_at, last_event_id, last_event_created_at ) VALUES ( 6, 45, ' 23:31:44', 453, ' 10:23:11' ) ON DUPLICATE KEY UPDATE last_session_id = IF (( last_session_created_at < VALUES ( last_session_created_at ) OR ( last_session_id = VALUES ( last_session_id ) AND last_event_created_at < VALUES ( last_event_created_at ))), VALUES ( last_session_id ), last_session_id ), last_session_created_at = IF (( last_session_created_at < VALUES ( last_session_created_at ) OR ( last_session_id = VALUES ( last_session_id ) AND last_event_created_at < VALUES ( last_event_created_at ))), VALUES ( last_session_created_at ), last_session_created_at ), last_event_id = IF (( last_session_created_at < VALUES ( last_session_created_at ) OR ( last_session_id = VALUES ( last_session_id ) AND last_event_created_at < VALUES ( last_event_created_at ))), VALUES ( last_event_id ), last_event_id ), last_event_created_at = IF (( last_session_created_at < VALUES ( last_session_created_at ) OR ( last_session_id = VALUES ( last_session_id ) AND last_event_created_at < VALUES ( last_event_created_at ))), VALUES ( last_event_created_at ), last_event_created_at ) Īside from the fact this looks a bit crazy (people who obsess about DRY code will be weeping) it also suffers from the order of evaluation problem I described earlier.

To give you an example, this query won’t produce the expected result: But they’re not: the assignments happen in the order they appear in the query. I was wrongly under the impression that the updates took place in one mass-assignment after the entire query had been interpreted by MySQL. This works by checking if the last_event_created_at timestamp of the event being updated is newer than the current timestamp, if it is then the new value is assigned to the field in the update, otherwise the current value is used.Īn important thing to keep in mind when using this approach is that the order in which you update your fields is very important. Try (Connection connection = ds.INSERT INTO daily_events ( created_on, last_event_id, last_event_created_at ) VALUES ( '', 23, ' 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 ), last_event_created_at = IF ( last_event_created_at < VALUES ( last_event_created_at ), VALUES ( last_event_created_at ), last_event_created_at ) ds is an entity of .jdbc.MysqlDataSource A user user_id = 1 gives a rating of 5 to a book book_id = 1000. In the following example, the primary key is the joint primary keys of book_id and user_id. If the user has already rated it, his previous rating will be updated. If the user has not yet rated the book, a new rating will be created. Use this statement when you create data or update data.įor example, you need to update the ratings table to include the user's ratings for the book.Therefore, it is not recommended to use the INSERT ON DUPLICATE KEY UPDATE statement in tables with multiple unique keys unless you can guarantee that there is only one row of conflict.

If there are more than one row of conflicts, only one row will be updated. This statement updates the data if any UNIQUE KEY (including the primary key) conflicts are detected.
#MYSQL INSERT ON DUPLICATE KEY UPDATE HOW TO#
This document describes how to use the following SQL statements to update the data in TiDB with various programming languages:
