Quantcast
Channel: MySQL Forums - InnoDB
Viewing all articles
Browse latest Browse all 1957

Insert on duplicate key update only if columns aren't changed (3 replies)

$
0
0
I have an insert statement something like the following:

insert into code_value (code_set,code_value,active_ind,display,display_key,updt_dt_tm)
values (1,1,1,"test2","TEST2",CURRENT_TIMESTAMP)
on duplicate key update
active_ind = 1,
display = "test2",
display_key = "TEST2",
updt_dt_tm = if(**WHAT GOES HERE**,CURRENT_TIMESTAMP,**WHAT GOES HERE**);

The primary keys of the table are code_set and code_value. So basically, it's supposed to work as follows:

Case 1: There is no row in the table where code_set = 1 and code_value = 1.

In this case, it will just insert the row, no problems.

Case 2: There is already a row in the table where code_set = 1 and code_value = 1, and one of active_ind, display, or display_key is different than the values that are trying to be inserted.

Here, we will update the existing row in the table, setting the updt_dt_tm column to CURRENT_TIMESTAMP, to reflect the fact that the row was actually updated.

Case 2: There is already a row in the table where code_set = 1 and code_value = 1, and none of active_ind, display, or display_key are different than the values that are trying to be inserted.

Here, we want to update the existing row in the table, but leave updt_dt_tm alone, because no true update has been done. That's where the IF() function comes in; I want to update the updt_dt_tm column only if one of the other columns is different than the values we're trying to insert.

Any ideas?

Viewing all articles
Browse latest Browse all 1957


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>