我有下表
+--------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+-------+
| image_id | int(11) | YES | | NULL | |
| image_status | bit(3) | YES | | NULL | |
| image_result | varchar(4) | YES | | NULL | |
+--------------+------------+------+-----+---------+-------+
image_id 和 image_status 列填充了值。 image_result中的值为NULL。
我想根据以下条件将以下值插入 image_result 列(我想更新表中的所有行)-
- 如果 image_status = '0' 或 image_status = '3' 那么 image_result = “通过”
- 如果 image_status = '1' 或 image_status = '4' 那么 image_result = '警告'
- 如果 image_status = '2' 那么 image_result = '失败'
我该怎么做?
最佳答案
UPDATE table
SET image_result = CASE
WHEN image_status = 0 OR image_status = 3 THEN 'Pass'
WHEN image_status = 1 OR image_status = 4 THEN 'Warm'
ELSE 'Fail'
END
关于MYSQL - 根据条件更新新列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12798520/