Why for table like this:
create table tbl (
id int not null auto_increment,
key1 int not null,
key2 int not null,
value0 double,
value1 double,
key(id),
unique(key1,key2)
);
Following query is not storing prev. value of ‘value0’ to ‘value1’ when updating existing record? Is it possible at all?
insert into tbl (key1, key2, value0, value1)
values (...), (...)
, ...
, (...)
on duplicate key update value0=values(value0), value1=if(value0<>values(value0),value0, value1);
UPDATE updates the values sequentially (both in UPDATE and in DUPLICATE UPDATE statement).
This means that if you refer a value in your update statement, that was updated in the same statement, you’ll get the updated value, and not the original one.
Because of this in the statement
value0 always equeals to values(value0) – it was updates in the previous part of the update statement. To make it work as you expect you have to switch the positions of the statements: