3

我有下表

+--------------+------------+------+-----+---------+-------+
| 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' OR image_status = '3' 那么 image_result = 'Pass'
  • 如果 image_status = '1' OR image_status = '4' 那么 image_result = 'Warn'
  • 如果 image_status = '2' 那么 image_result = 'Fail'

我该怎么做以上?

4

3 回答 3

8
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 
于 2012-10-09T10:58:13.070 回答
2
UPDATE TableName
SET image_result = CASE 
                     WHEN image_status = '0' OR image_status = '3' THEN 'PASS'  
                     WHEN image_status = '1' OR image_status = '4' THEN 'Warn' 
                     WHEN image_status = '2' THEN 'Fail'
                   END
WHERE image_status IN('0', '1', '2', '3', '4'); 
于 2012-10-09T11:00:21.620 回答
0
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 'Warn'
     when image_status = '2'                       then 'Fail'
End
于 2012-10-09T10:57:52.420 回答