patternsqlMinor
MySQL set column value if another column's new value is different than current one
Viewed 0 times
newcolumnthanvaluedifferentmysqloneanothercurrentset
Problem
I have a table that has a column
I have come up with this query, but
Is it possible check the new and old value in a single query?
mycolumn and a column that has a boolean flag mycolumn_is_new. I want to set the second one to true when an UPDATE query changes the value of the first one. The user can type the same data or new data.I have come up with this query, but
mycolumn_is_new is always set to false :UPDATE mytable SET
mycolumn = '1234',
mycolumn_is_new = IF(mycolumn <> '1234', TRUE, FALSE)
WHERE id = 321Is it possible check the new and old value in a single query?
Solution
All expressions in the SET section are evaluated in the order in which they are written. So
Is it possible check the new and old value in a single query?
Yes, using user-defined variable. Save old, assign new, use old from variable and new from field. The simplest task need those method - switch the values of two fields in one UPDATE.
mycolumn in the second expression is equal to the value set in the first expression. Simply switch expressions - mycolumn_is_new = first. Is it possible check the new and old value in a single query?
Yes, using user-defined variable. Save old, assign new, use old from variable and new from field. The simplest task need those method - switch the values of two fields in one UPDATE.
Context
StackExchange Database Administrators Q#208983, answer score: 6
Revisions (0)
No revisions yet.