skip to Main Content

I want to update two records from the age column in the table below, for this I use the following command:

enter image description here

UPDATE `persons` SET `Age` =( CASE WHEN FirstName = 'User 2' THEN 10 WHEN FirstName = 'User 3' THEN 12 END ) WHERE LastName = 'User'; 

According to the picture below, the rest of the age records are equal to null

enter image description here

Please guide me to solve this problem

2

Answers


  1. use this query:

    UPDATE `persons` SET `Age` = ( CASE WHEN FirstName = 'User 2' THEN 10 WHEN FirstName = 'User 3' THEN 12 ELSE Age END ) WHERE LastName = 'User'; 
    
    Login or Signup to reply.
  2. I recreated your table:
    enter image description here

    The updated statement would be for your case:

    UPDATE `persons` 
    SET `Age` = CASE 
                   WHEN FirstName = 'User 2' THEN 10 
                   WHEN FirstName = 'User 3' THEN 12 
                   ELSE Age
                END
    WHERE LastName LIKE 'User%';
    

    Result:
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search