Yes, that's possible - you can use INSERT ... ON DUPLICATE KEY UPDATE.

Using your example:

INSERT INTO table (id,Col1,Col2) VALUES (1,1,1),(2,2,3),(3,9,3),(4,10,12)
ON DUPLICATE KEY UPDATE Col1=VALUES(Col1),Col2=VALUES(Col2);
Answer from Michiel de Mare on Stack Overflow
Discussions

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
mysql - How can I UPDATE multiple ROWs in a Single Query with multiple WHERE clauses? - Database Administrators Stack Exchange
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 ... More on dba.stackexchange.com
🌐 dba.stackexchange.com
February 18, 2018
mysql - How to update a multiple rows all at once with a procedure.? - Database Administrators Stack Exchange
Im trying to do a procedure to update an (for now) empty column recently added. In this new column, i will put the values of the result obtained by a join query. To explain it better, i have this More on dba.stackexchange.com
🌐 dba.stackexchange.com
mysql - Update many rows in a table with a single statement? - Database Administrators Stack Exchange
What is the easiest way to update many rows in a table? I have a csv file that looks like this: |primary_key |value| | 1 | xyz| | 2 | abc| | 3 | def| ... Rows with th... More on dba.stackexchange.com
🌐 dba.stackexchange.com
April 26, 2020
🌐
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 - UPDATE users SET firstName = (case when id = 1 then 'encryptedFirstName1' when id = 2 then 'encryptedFirstName2' when id = 3 then 'encryptedFirstName3' end) WHERE id in (1, 2, 3); Looks nice doesn’t it? But one senior developer at my first job, always asked me, can we do it differently?
🌐
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?

🌐
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 - 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 · MariaDB's VALUES clause is shorter as it doesn't use the ROW keyword at all
Find elsewhere
🌐
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.
🌐
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 ...
🌐
Dirask
dirask.com › posts › MySQL-Update-multiple-rows-at-once-13qV9p
MySQL - Update multiple rows at once
In this article, we would like to show you how to update multiple rows at once in MySQL. Quick solution: Practical example To show you how to update multiple ro...
🌐
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
If the where clause returns more than one row, the update sql statement will update multiple rows. ... Stake: Online Casino games - Play & Win Online. Play the best online casino games, slots & live casino games!
🌐
Etutorialspoint
etutorialspoint.com › index.php › mysql-exercises › mysql-update-multiple-rows-in-one-query
MySQL update multiple rows in one query
The best way to update multiple rows in just one statement is use CASE WHEN ELSE statement. In this, the statement will update the matched case and end otherwise, like- UPDATE 'table_name' SET 'field_name' = CASE 'id' WHEN '1' THEN 'value 1' WHEN '2' THEN 'value 2' WHEN '3' THEN 'value 3' ELSE ...
🌐
Edureka Community
edureka.co › home › community › categories › web development › php › mysql - update multiple rows with different...
MySQL - UPDATE multiple rows with different values in one query | Edureka Community
June 1, 2020 - Hello, I wanted to know how to UPDATE multiple rows with different values and I just don't get it ... in the WHERE and in the IF condition.Any ideas?
🌐
TutorialsPoint
tutorialspoint.com › update-multiple-rows-in-a-single-column-in-mysql
Update multiple rows in a single column in MySQL?
To update multiple rows in a single column, use CASE statement. Let us first create a table − · mysql> create table updateMultipleRowsDemo -> ( -> StudentId int, -> StudentMathScore int -> ); Query OK, 0 rows affected (0.63 sec)
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
;
Top answer
1 of 2
1

Finally i got it, after several tries i manage to get what i needed, so i'll share my answer.

First i made one procedure to update one single row in the column:

delimiter //
create procedure update_amount_products (in id int)
begin
update categories set products_amount= 
           (SELECT DISTINCT COUNT( products_to_categories.products_id ) 
FROM categories_description
INNER JOIN products_to_categories ON products_to_categories.categories_id = categories_description.categories_id
INNER JOIN products_description ON products_description.products_id = products_to_categories.products_id
WHERE categories_description.categories_id =id)
WHERE categories_id = id;
end
//

DELIMITER ;

Then i realized i could handle the whole column by calling the procedure with another procedure with a loop on it

DELIMITER $$
CREATE PROCEDURE update_products_amount()

   BEGIN
      DECLARE a INT Default 1 ;
      DECLARE category_length int;

      select max(categories_id) into category_length from categories_description;

      simple_loop: LOOP
         SET a=a+1;

         call update_amount_products(a);

         IF a >= category_length THEN
            LEAVE simple_loop;
         END IF;
   END LOOP simple_loop;
END $$
DELIMITER ;

commit;

Finally, all i have to do now, is to call my last procedure and voilá, all my column will be automatically updated.

call update_products_amount();
2 of 2
0

Not quite sure what you are trying to achieve bit the UPDATE statement would appear to do the job

To update a single known product:

UPDATE PRODUCTS SET PRODUCT_AMOUNT = 42 WHERE PRODUCTS_ID = 1

You can update several at once depending on the WHERE clause and you can also set it to a value from another table.

🌐
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 - It is ok to use multiple UPDATE statements if we have a few records in the table. Suppose there are millions of rows in the table. Some of the ways to update the table are listed below.
🌐
Scaler
scaler.com › home › topics › how to update multiple columns in mysql?
How To Update Multiple Columns in MySQL? - Scaler Topics
July 7, 2023 - Now, let us see how one can update multiple columns in MySQL using the REPLACE string. ... Once you execute the SELECT statement, you shall see the row with Class_ID as 36 is updated, as seen below:
Top answer
1 of 2
12

First here is sample data

mysql> drop table if exists mytable;
Query OK, 0 rows affected (0.03 sec)

mysql> create table mytable
    -> (
    ->     id int not null,
    ->     value VARCHAR(255),
    ->     primary key (id)
    -> );
Query OK, 0 rows affected (0.06 sec)

mysql> insert into mytable (id) values (1),(2),(3);
Query OK, 3 rows affected (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from mytable;
+----+-------+
| id | value |
+----+-------+
|  1 | NULL  |
|  2 | NULL  |
|  3 | NULL  |
+----+-------+
3 rows in set (0.00 sec)

mysql>

Here is the new query

update mytable A inner join
(
    SELECT 1 id,'xyz' value UNION
    SELECT 2   ,'abc'       UNION
    SELECT 3   ,'def'

) B USING (id)
SET A.value = B.value;

Here is the new query executed

mysql> update mytable A inner join
    -> (
    ->     SELECT 1 id,'xyz' value UNION
    ->     SELECT 2   ,'abc'       UNION
    ->     SELECT 3   ,'def'
    -> ) B USING (id)
    -> SET A.value = B.value;
Query OK, 0 rows affected (0.06 sec)
Rows matched: 3  Changed: 0  Warnings: 0

mysql> select * from mytable;
+----+-------+
| id | value |
+----+-------+
|  1 | xyz   |
|  2 | abc   |
|  3 | def   |
+----+-------+
3 rows in set (0.00 sec)

mysql>
2 of 2
3

Assuming that you don't want to load the data from the CSV file into a database table and then do a correlated UPDATE,

UPDATE mytable t
   SET value = (SELECT value
                  FROM tbl_with_csv_data csv
                 WHERE csv.primary_key = t.primary_key)
 WHERE EXISTS( SELECT 1
                 FROM tbl_with_csv_data csv
                 WHERE csv.primary_key = t.primary_key)

then you should be able to use a CASE

UPDATE mytable t
   SET value = CASE WHEN primary_key = 1 THEN 'xyz'
                    WHEN primary_key = 2 THEN 'abc'
                    WHEN primary_key = 3 THEN 'def'
                    ELSE value
                END
 WHERE primary_key IN (1,2,3);