UPDATE multiple rows with different values in one query in MySQL

I am trying to understand how to UPDATE multiple rows with different values and I just don’t get it. The solution is everywhere but to me it looks difficult to understand.

For instance, three updates into 1 query:

UPDATE table_users
SET cod_user="622057"
    , date="12082014"
WHERE user_rol="student"
    AND cod_office="17389551"; 

UPDATE table_users
SET cod_user="2913659"
    , date="12082014"
WHERE user_rol="assistant"
    AND cod_office="17389551"; 

UPDATE table_users
SET cod_user="6160230"
    , date="12082014"
WHERE user_rol="admin"
    AND cod_office="17389551"; 

I read an example, but I really don’t understand how to make the query. i.e:

UPDATE table_to_update
SET cod_user= IF(cod_office="17389551",'622057','2913659','6160230')
    ,date = IF(cod_office="17389551",'12082014')
WHERE ?? IN (??) ;

I’m not entirely clear how to do the query if there are multiple condition in the WHERE and in the IF condition..any ideas?

8 Answers
8

Leave a Comment