Is there a way to get value before and update query in update query itself?
For example:
UPDATE t SET field1 = 'new', field2 = IF (field2 = field1, 'new', field2))
In ‘field2 = field1’ I want to check the previous value of field. Mysql works in a such way that it updates field1 and then will make comparison with the new value, so the comparison always will be “IF (field2 = ‘new’, …”
So I would like something like
UPDATE t SET field1 = 'new', field2 = IF (field2 = PREVIOUS_VALUE(field1), 'new', field2))
Updated:
swapping may not work sometimes in more complex examples, when I want it like this:
UPDATE t SET field1 = IF (field1 = PREVIOUS_VALUE(field2), 'new', field1)), field2 = IF (field2 = PREVIOUS_VALUE(field1), 'new', field2))
Just rearrange those sets.
Demo: http://sqlfiddle.com/#!2/bb265/1