开发者

MySQL SET IF Statement

开发者 https://www.devze.com 2023-03-02 04:27 出处:网络
Can someone show me the correct mysql syntax to do the following: Update a column in a table with 1 of 3 values:

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"
0

精彩评论

暂无评论...
验证码 换一张
取 消