MySQL SET IF Statement
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 E开发者_如何学Clse 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?
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"
精彩评论