3

I have the following table

+--------------+------------+------+-----+---------+-------+
| 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 and image_status columns are populated with values. The values in image_result are all NULL.

I want to insert the following values into the image_result column based on the following conditions (I want to update all the rows in the table)-

  • if image_status = '0' OR image_status = '3' then image_result = 'Pass'
  • if image_status = '1' OR image_status = '4' then image_result = 'Warn'
  • if image_status = '2' then image_result = 'Fail'

How do I do the above?

3 Answers 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 
Sign up to request clarification or add additional context in comments.

Comments

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'); 

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.