Reputation: 8106
Can someone show me the correct mysql syntax to do the following:
Update a column in a table with 1 of 3 values:
If col_A = 4 set col_Z to col_A If col_B = 4 set col_Z to col_B Else set col_Z to NULL (or leave alone because col_Z is initialized to NULL)
Here's what I have:
Update my_table
SET col_Z = IF(col_A = 4, col_A, IF(col_B = 4, col_B, NULL))
WHERE id = "001"
IS this correct?
Upvotes: 7
Views: 36940
Reputation: 76537
Yes, it looks correct.
The following code will be simpler though.
UPDATE my_table
SET col_Z = IF(col_A = 4 OR col_B = 4, 4, NULL)
WHERE id = "001"
Upvotes: 23