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
Discussions

mysql - Updating multiple rows with different values in one query - Database Administrators Stack Exchange
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, two update... More on dba.stackexchange.com
🌐 dba.stackexchange.com
June 28, 2014
Update a single column on multiple rows with one SQL query - Stack Overflow
An important piece of information ... the same column across a specific set of rows. ... Which RDBMS are you using? MySQL is not SQL Server. ... @StevenRogers, you did not accept an answer yet. It does not have to be mine, but please accept an answer so the question is resolved. ... You could use the MERGE statement which is in the SQL:2003 standard and available in Transact-SQL since SQL Server 2008: MERGE mytable USING (VALUES (23, 'FOD'), ... More on stackoverflow.com
🌐 stackoverflow.com
sql server - MS SQL How to Update multiple rows with single value? - Stack Overflow
Could anyone tell me how to update a number of rows with a same value..? for example if I have an employee table like, employee id salary 1 100 2 230 3 ... More on stackoverflow.com
🌐 stackoverflow.com
sql server - SQL update multiple rows in destination table with same id but different values from source table - Database Administrators Stack Exchange
I have two different tables with a common column called id: Table1 ---- ------- id | Date ---- ------- 1 null 1 null 2 null 2 null 2 null 2 null 3 null 4 ... More on dba.stackexchange.com
🌐 dba.stackexchange.com
August 10, 2017
🌐
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 - But how? Well, did you know, that we can use JOIN in the UPDATE statement? Yes, we can. So, we will join this data from this select statements and set our firstName as new value and id as our condition like this
🌐
W3Schools
w3schools.com › sql › sql_update.asp
SQL UPDATE Statement
SQL Examples SQL Editor SQL Quiz ... is used to update or modify one or more records in a table. UPDATE table_name SET column1 = value1, column2 = value2, ......
🌐
GeeksforGeeks
geeksforgeeks.org › sql › how-to-update-multiple-records-using-one-query-in-sql-server
How to Update Multiple Records Using One Query in SQL Server? - GeeksforGeeks
To update multiple records of a ... syntax: UPDATE table_name SET column_value = CASE column_name WHEN 'column_name1' THEN column_value1 WHEN 'column_name2' THEN column_value2 ELSE column_value END WHERE column_name IN('column_name1', 'column_name2');...
Published   July 23, 2025
Find elsewhere
Top answer
1 of 5
11

You could use the MERGE statement which is in the SQL:2003 standard and available in Transact-SQL since SQL Server 2008:

MERGE mytable
USING (VALUES (23, 'FOD'),
              (47, 'ASD'),
              (83, 'FGH'),
              (88, 'JKL'),
              (92, 'QWE'),
              ( 9, 'BAR')) AS pairs(id2, data2)
     ON id = id2 
WHEN MATCHED 
     THEN UPDATE SET data = data2

The USING clause allows to specify a derived table using a table value constructor (See example under point D on that page).

Alternatively, the more commonly implemented SQL:92 standard syntax to do this would be:

UPDATE mytable
SET    data = 
       CASE id
            WHEN 23 THEN 'FOD'
            WHEN 47 THEN 'ASD'
            WHEN 83 THEN 'FGH'
            WHEN 88 THEN 'JKL'
            WHEN 92 THEN 'QWE'
            WHEN  9 THEN 'BAR'
       END
WHERE  id IN (23, 47, 83, 88, 92, 9);

The obvious downside is that you end up specifying the id values twice. You could do without the WHERE clause and add ELSE data in the CASE construct, but then you would actually update all rows, which is inefficient and may have undesired side-effects (via triggers).

2 of 5
4

I'm assuming that you are in MySQL. You can use a combination of "Field" and "Elt" functions to do what you need in a single query (beside the CASE WHEN THEN WHEN THEN WHEN THEN WHEN THEN WHEN THEN WHEN THEN WHEN THEN END or IF(<condition>,<output>,if(<condition2>,<output2>, if())) methods.

UPDATE [table] SET DATA=ELT(FIELD(ID,
   13, 14, 15, 16, 17, 18, 19),'FOO', 'ASD', 'FGH', 'JKL', 'QWE', 'BAR');

This is similar to the DECODE() function in Oracle, which I wish had counterparts in other DBMS's.

🌐
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)
🌐
DEV Community
dev.to › chidioguejiofor › scenario-1-making-updates-to-multiple-fields-56hl
Scenario 1: When You Need to Update Multiple Rows with Different Values – The Smart Way! - DEV Community
September 30, 2024 - Updating multiple rows in a database is a common task when managing data, whether you're setting the same value across several rows or handling different updates for each one.
🌐
Quora
quora.com › How-can-you-update-multiple-values-in-one-column-in-SQL
How to update multiple values in one column in SQL - Quora
Answer (1 of 5): Yes, You can update the multiple values in one column, Simply use CASE Statement. Example: My table name is Map, New column name is Country_code. Reference column name is Country_Name.
🌐
DataCamp
datacamp.com › tutorial › update-multiple-columns-sql
How to Update Multiple Columns in SQL | DataCamp
November 8, 2024 - If you omit the WHERE clause, the update will apply to all rows in the table, which can lead to unintentional data modification. You can use the COALESCE function to handle NULL values by providing default values when updating. You can perform updates by joining tables, allowing you to update columns based on related data from a different table. ... 38.2KLearn how to build your own SQL reports and dashboards, plus hone your data exploration, cleaning, and validation skills.
Top answer
1 of 2
8

Couldn't find a SQL Server 2008 fiddle engine so I had to opt for a SQL Server 2014 ... so not sure if the following will work in SQL Server 2008, but fwiw ...

Setup some sample data:

create table Table1(id int, Date datetime null);
create table Table2(id int, Date datetime);

insert Table1 values (1,null)
insert Table1 values (1,null)
insert Table1 values (2,null)
insert Table1 values (2,null)
insert Table1 values (2,null);

insert Table2 values (1,'2013-01-29 08:50:00.000')
insert Table2 values (1,'2013-01-29 15:28:00.000')
insert Table2 values (2,'2013-01-31 11:56:00.000')
insert Table2 values (2,'2013-03-11 16:08:00.000')
insert Table2 values (2,'2013-01-31 14:04:00.000');

Keeping in mind that we haven't been provided (yet) with any means to determine which rows to match between Table1 and Table2 for a given id value, I'll just let row_number() generate a 'matching' rowid.

And then we'll make use of SQL Server's ability to update Table1 via a derived table definition:

update T1 
set    T1.Date=T2.Date

from   (select row_number() over(partition by id order by Date) as rowid,
               id,
               Date
        from   Table1 
        where  Date is NULL) T1

join   (select row_number() over(partition by id order by Date) as rowid,
               id,
               Date
        from   Table2) T2

on      T1.id    = T2.id
and     T1.rowid = T2.rowid;

And the results:

select * from Table1;

id  Date
--- --------------------
1   2013-01-29T08:50:00Z
1   2013-01-29T15:28:00Z
2   2013-01-31T11:56:00Z
2   2013-01-31T14:04:00Z
2   2013-03-11T16:08:00Z

And here's a SQL Fiddle for the above.

2 of 2
3

You stated that the order of the matching matters but it seems like you don't have anything to ORDER BY in table 1 to create a guaranteed order to match the other table and there is no way in SQL Server to order the rows after insertion date, because information about that is not stored. With this in mind it’s not possible to do a matching with the result you want. There is a solution to update the rows with an arbitrary match within each id. If that would be good enough.

UPDATE t 
SET    t.[date] = tt.[date] 
FROM   (SELECT *, 
               Row_number() 
                 OVER ( 
                   partition BY id 
                   ORDER BY [date]) AS rno 
        FROM   Table1) AS t 
       INNER JOIN (SELECT *, 
                          Row_number() 
                            OVER ( 
                              partition BY id 
                              ORDER BY [date]) AS rno 
                   FROM   Table2) AS tt 
               ON t.id = tt.id 
                  AND t.rno = tt.rno 

This solution will match all rows individually but can't guarantee the order.

DB Fiddle

🌐
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 ...
🌐
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 - MariaDB's VALUES clause is shorter as it doesn't use the ROW keyword at all · UPDATE table_name SET changed_col = temp_data.column1 FROM (VALUES ('key1', 'val1'), ('key2', 'val2'), .... ) as temp_data WHERE comparison_col = temp_data.column0 · Postgres definitely have the best syntax among the three, as it support aliasing column names for VALUES syntax, just as SQL Server did
🌐
SitePoint
sitepoint.com › databases
Multiple UPDATE, same column, different values, different WHERE conditions - Databases - SitePoint Forums | Web Development & Design Community
August 9, 2008 - I’ve found plenty of info around about updating multiple rows with the same value using “WHERE columname IN”, and I’ve got that down. But, I’m needing to UPDATE a column in multiple rows with a different value for each WHERE condition. My updates are being done as individual queries like this: UPDATE tablename SET widget='zamu-xxx' WHERE widget='zamu'; UPDATE tablename SET widget='flabu-yyy' WHERE widget='flabu'; Is there any way to do something like this in a single query?
🌐
Quora
quora.com › In-SQL-is-it-possible-to-update-entries-in-multiple-rows-of-the-same-column-in-a-single-statement
In SQL, is it possible to update entries in multiple rows of the same column in a single statement? - Quora
Answer (1 of 8): > In SQL, is it possible to update entries in multiple rows of the same column, in a single statement? UPDATE: Thanks to User-9594919546600660871 and Swastik Bhat for reminding me about the [code ]CASE[/code] construct. It’s defined in SQL-92, so every SQL DB worthy of the ...
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
;
🌐
Quora
quora.com › How-do-you-update-multiple-values-in-SQL
How to update multiple values in SQL - Quora
Answer (1 of 6): If you mean “How does one update multiple column values within a row or rows in a table with a single statement?” then witness: UPDATE mytab SET col1=val1, col2=(col2*1.5), col3=(col4 / 3) WHERE keycol IN (1223, 34234, 5332 ); ...