You can do it this way:

UPDATE table_users
    SET cod_user = (case when user_role = 'student' then '622057'
                         when user_role = 'assistant' then '2913659'
                         when user_role = 'admin' then '6160230'
                    end),
        date = '12082014'
    WHERE user_role in ('student', 'assistant', 'admin') AND
          cod_office = '17389551';

I don't understand your date format. Dates should be stored in the database using native date and time types.

Answer from Gordon Linoff on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › mysql › how-to-update-multiple-rows-in-a-single-query-in-mysql
How to UPDATE Multiple ROWs in a Single Query in MySQL? - GeeksforGeeks
July 23, 2025 - Therefore updating multiple rows in a single query in MySQL can be efficiently done using the UPDATE statement having a WHERE clause in it. This WHERE clause is used to specify the criteria for records that you want to update.
Discussions

sql - Multiple Updates in MySQL - Stack Overflow
I know that you can insert multiple rows at once, is there a way to update multiple rows at once (as in, in one query) in MySQL? Edit: For example I have the following Name id Col1 Col2 Row1 ... More on stackoverflow.com
🌐 stackoverflow.com
Can I Update Multiple Rows at Once?
You can do something like: Update testtbl set a ='1' where SERIAL IN (1,2) More on reddit.com
🌐 r/mysql
8
1
May 7, 2021
Updating multiple columns and multiple rows with one MySQL query - Databases - SitePoint Forums | Web Development & Design Community
Hi SitePoint members I have been perusing through the solutions for “updating multiple rows with one query”, but I have a pressing question: How would one “SET” multiple column values with one query? Here is my example…the normal update command would be: UPDATE table_FooBar SET ... More on sitepoint.com
🌐 sitepoint.com
0
April 5, 2011
mysql - How can I UPDATE multiple ROWs in a Single Query with multiple WHERE clauses? - Database Administrators Stack Exchange
How can I simplify these Queries UPDATE `table` SET `col1` = 'abc', `col2` = 'xyz' WHERE `col3` = '1'; UPDATE `elbat` SET `col1` = `a`, `col2` = 'x' WHERE `col3` = '1'; UPDATE `elbat` SET `col1` =... More on dba.stackexchange.com
🌐 dba.stackexchange.com
February 18, 2018
🌐
DEV Community
dev.to › azophy › how-to-update-multiple-rows-based-on-list-of-key-val-pairs-in-mysql-mariadb-postgresql-4lpp
How to update multiple rows based on list of key-val pairs (in MySQL, MariaDB, & PostgreSQL) - DEV Community
September 29, 2023 - UPDATE table_name SET changed_col = temp_data.column1 FROM (VALUES ROW('key1', 'val1'), ROW('key2', 'val2'), .... ) as temp_data WHERE comparison_col = temp_data.column0 · https://dev.mysql.com/doc/refman/8.0/en/values.html
🌐
TablePlus
tableplus.com › blog › 2018 › 11 › how-to-update-multiple-rows-at-once-in-mysql.html
How to update multiple rows at once in MySQL? | TablePlus
November 12, 2018 - INSERT INTO students (id, score1, score2) VALUES (1, 5, 8), (2, 10, 8), (3, 8, 3), (4, 10, 7) ON DUPLICATE KEY UPDATE score1 = VALUES(score1), score2 = VALUES(score2); Need a good GUI Tool for MySQL? TablePlus is a modern, native tool with an elegant UI that allows you to simultaneously manage ...
Find elsewhere
🌐
Quora
quora.com › How-can-I-update-multiple-rows-columns-in-MySQL-with-one-query
How to update multiple rows & columns in MySQL with one query - Quora
Yes, you can update multiple rows in a single statement. It basically depends on the WHERE condition which you are applying in your SQL query.
🌐
w3resource
w3resource.com › mysql › update-table › update-table.php
MySQL UPDATE - w3resource
February 28, 2026 - Only rows where the value of the purch_price column is greater than 50 will be affected by this update. ... MySQL UPDATE command can be used to update multiple columns by specifying a comma separated list of column_name = new_value.
🌐
Reddit
reddit.com › r/mysql › can i update multiple rows at once?
r/mysql on Reddit: Can I Update Multiple Rows at Once?
May 7, 2021 -

I've looked a little for this and it's not jumping out at me. Is it possible to update multiple rows at once with one query? For instance, can I turn these two queries:

queryStr = "update TEST_TBL set VAL1 = '1' WHERE SERIAL = 1;"
queryStr = "update TEST_TBL set VAL1 = '1' WHERE SERIAL = 2;"

into something like this:

queryStr = "update TEST_TBL set VAL1 = '1' WHERE SERIAL = 1, SERIAL = 2;"

Obviously I know that won't work, but is there a way to kill two birds with one query?

🌐
SitePoint
sitepoint.com › databases
Updating multiple columns and multiple rows with one MySQL query - Databases - SitePoint Forums | Web Development & Design Community
April 5, 2011 - Here is my example…the normal update command would be: UPDATE table_FooBar SET answerOne='$ans1Val', answerTwo='$ans2Val', answerThree='$ans3Val' WHERE member_id='$memberid' AND question_id='$questionid'; Now I have to do this for up to 20 ...
🌐
TutorialsPoint
tutorialspoint.com › update-multiple-rows-in-a-single-column-in-mysql
Update multiple rows in a single column in MySQL?
mysql> UPDATE updateMultipleRowsDemo -> SET StudentMathScore= CASE StudentId -> WHEN 10001 THEN 45 -> WHEN 10002 THEN 52 -> WHEN 10003 THEN 67 -> END -> WHERE StudentId BETWEEN 10001 AND 10003; Query OK, 3 rows affected (0.19 sec) Rows matched: 3 Changed: 3 Warnings: 0
🌐
Quora
quora.com › How-can-you-update-multiple-records-in-MySQL-with-one-query
How to update multiple records in MySQL with one query - Quora
Syntax (MySQL 8+ supports VALUES() in derived table or use UNION SELECT): UPDATE target JOIN ( SELECT 1 AS id, 'value1' AS new_val UNION ALL SELECT 2, 'value2' UNION ALL SELECT 3, 'value3' ) AS src ON target.id = src.id SET target.col_to_change ...
🌐
Delft Stack
delftstack.com › home › howto › mysql › update multiple columns in multiple rows with different values in mysql
How to Update Multiple Columns in Multiple Rows With Different Values in MySQL | Delft Stack
February 16, 2024 - You may consider MySQL Documentation for further assistance. ... UPDATE students std JOIN ( SELECT 1 AS ID, 78 AS JavaScore, 73 AS PythonScore UNION ALL SELECT 2 AS ID, 83 AS JavaScore, 88 AS PythonScore UNION ALL SELECT 3 AS ID, 89 AS JavaScore, 97 AS PythonScore UNION ALL SELECT 4 AS ID, 58 AS JavaScore, 78 AS PythonScore ) temp ON std.ID = temp.ID SET std.JavaScore = temp.JavaScore, std.PythonScore = temp.PythonScore;
🌐
Scaler
scaler.com › home › topics › how to update multiple columns in mysql?
How To Update Multiple Columns in MySQL? - Scaler Topics
July 7, 2023 - There are scenarios where we don't ... update multiple columns in MySQL with a single query. With the help of the UPDATE statement, the user can update the data that justifies the consideration in a table. The value could be modified in more than one column for a single row or multiple ...
🌐
Medium
medium.com › geekculture › update-multiple-rows-in-sql-with-different-values-at-once-7d2eddb0b85f
Update multiple rows in SQL with different values at once | by Tadej Golobic | Geek Culture | Medium
June 12, 2021 - Ok, so now we generated all this stuff that needs to be inserted / updated in users table. But how? Well, did you know, that we can use JOIN in the UPDATE statement? Yes, we can.
🌐
MySQL
dev.mysql.com › doc › refman › 9.6 › en › update.html
MySQL :: MySQL 9.6 Reference Manual :: 15.2.17 UPDATE Statement
If the ORDER BY clause is specified, the rows are updated in the order that is specified. The LIMIT clause places a limit on the number of rows that can be updated. For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions.
Top answer
1 of 2
6

Remarks

It is possible to update rows based on some condition. It is also possible to update multiple tables in one statement in MySQL.

Whether the latter is a good idea is debatable, though. The target tables would be joined together for the update, and when I say "joined", I mean it in a broader sense: you do not have to specify a joining condition, in which case theirs would be a cross join. In a cross join, when at least one of the tables has more than one row, the other table will inevitably have its rows duplicated in the joined set. If both have multiple rows, both will have them multiplied. Somewhat counter-intuitively, MySQL will still update each affected row just once, yet I would refrain from multi-table updates in such scenarios, even if solely because of the counter-intuitiveness.

Method 1

Anyway, moving on to your specific example, there is indeed no joining condition, only a filter on each table. You can specify those filters in the WHERE clause of the UPDATE. Now in order to select which value to update each column with, you can use a CASE expression. This is what the complete UPDATE statement might look like:

UPDATE
  A, B
SET
  A.col1 = 'abc',
  A.col2 = 'xyz',
  B.col1 = CASE B.col3
             WHEN '1' THEN 'a'
             WHEN '2' THEN 'b'
             WHEN '3' THEN 'c'
           END,
  B.col2 = CASE B.col3
             WHEN '1' THEN 'x'
             WHEN '2' THEN 'y'
             WHEN '3' THEN 'z'
           END
WHERE
  A.col3 = '1'
  AND B.col3 IN ('1', '2', '3')
;

You can see that you have to repeat the same set of conditions in a CASE expression both for B.col1 and for B.col2. Is there a way to avoid that?

Method 2

Yes, there is. You can arrange the target values for B.col1 and B.col2 as well as the filtering values for B.col3 as a derived table and join it to B in the UPDATE clause, like this:

UPDATE
  A,
  B
    INNER JOIN
    (
      SELECT 'a' AS col1, 'x' AS col2, '1' AS col3
      UNION ALL
      SELECT 'b', 'y', '2'
      UNION ALL
      SELECT 'c', 'z', '3'
    ) AS fltr ON B.col3 = fltr.col3
SET
  A.col1 = 'abc',
  A.col2 = 'xyz',
  B.col1 = fltr.col1,
  B.col2 = fltr.col2
WHERE
  A.col3 = '1'
;

The join is also acting as a filter for B, so you can omit the one in the WHERE clause.

You can find a demo for each method at db<>fiddle:

  • Method 1
  • Method 2

Better way

Finally, as have been remarked both at the beginning of this post and in the comments, you can have a separate UPDATE statement for each table. The result would be clear in intention both to the reader of your script and to the database engine. A simpler script enables the latter to have more options for optimisation.

Use either of the methods above for the table B update, but do both tables separately:

UPDATE
  A
SET
  A.col1 = 'abc',
  A.col2 = 'xyz'
WHERE
  A.col3 = '1'
;
UPDATE
  B
    INNER JOIN
    (
      SELECT 'a' AS col1, 'x' AS col2, '1' AS col3
      UNION ALL
      SELECT 'b', 'y', '2'
      UNION ALL
      SELECT 'c', 'z', '3'
    ) AS fltr ON B.col3 = fltr.col3
SET
  B.col1 = fltr.col1,
  B.col2 = fltr.col2
;

There is also another reason for splitting the updates in separate statements. Since for a single UPDATE statement the tables need to be joined, it is important that both tables have rows intended for the update. If one table has no matching rows, then, even if the other does, neither will be updated. This is because an empty set cross-joined to a non-empty set still results in an empty set.

So, the single UPDATE statement would have no rows to work with if at least one table had no rows matching the condition(s). That would not happen with separate UPDATEs, because each would work with its own table regardless of the contents of the other, therefore, absence of rows in one table would not affect the update of the other.

2 of 2
0

You can use below one for one table if you want to update many columns of one table.

UPDATE table
SET col1 = CASE WHEN col3 = 'name1' THEN 'a' 
            WHEN col3 = '2' THEN b 
            ELSE 0 
       END
 , col2 = CASE WHEN col3 = '1' THEN 'b' 
           WHEN col3 = 'name2' THEN 'c' 
           ELSE '' 
      END
;
🌐
TutorialsPoint
tutorialspoint.com › How-can-we-update-columns-values-on-multiple-rows-with-a-single-MySQL-UPDATE-statement
How can we update columns values on multiple rows with a single MySQL UPDATE statement?
mysql> UPDATE tender SET rate = rate + 1000 WHERE tender_id >= 300; Query OK, 4 rows affected (0.07 sec) Rows matched: 4 Changed: 4 Warnings: 0 mysql> Select * from tender; +-----------+---------+------+ | tender_id | company | rate | +-----------+---------+------+ | 200 | ABC | 1000 | | 300 | ABD | 6000 | | 301 | ABE | 7000 | | 302 | ABF | 3500 | | 303 | ABG | 3600 | +-----------+---------+------+ 5 rows in set (0.00 sec) We can observe from the above result set that the values in multiple rows, having tender_id >= 300, has been updated.
🌐
Kavoir
kavoir.com › 2009 › 05 › mysql-update-multiple-rows-with-one-single-query.html
MySQL: Update Multiple Rows or Records with One Single Query – Kavoir LLC
Below is an example of updating more than one rows in a table by the id field. The field_name field is updated to the corresponding value: value_1, value_2, or value_3 if the id equals to 1, 2, or 3: UPDATE `table_name` SET `field_name` = CASE `id` WHEN '1' THEN 'value_1' WHEN '2' THEN 'value_2' WHEN '3' THEN 'value_3' ELSE `field_name` END · Yet another way ============================================= There’s also another way of doing this: https://www.kavoir.com/2009/05/mysql-insert-if-doesnt-exist-otherwise-update-the-existing-row.html