Use the VALUES() function

INSERT INTO t (t.a, t.b, t.c)
VALUES ('key1','key2','value'), ('key1','key3','value2')
ON DUPLICATE KEY UPDATE
t.c = VALUES(t.c)

see http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

Answer from สžษ”ฤฑu on Stack Overflow
Discussions

How to run INSERT query with ON DUPLICATE KEY UPDATE in a single batch
what have you tried? did it work? did it produce an error message? did it insert but not update? can you show us the query? More on reddit.com
๐ŸŒ r/mysql
6
2
January 18, 2020
sql - MySQL ON DUPLICATE KEY UPDATE for multiple rows insert in single query - Stack Overflow
I have a SQL query where I want to insert multiple rows in single query. so I used something like: $sql = "INSERT INTO beautiful (name, age) VALUES ('Helen', 24), ('Katrina', 21), ('Samia'... More on stackoverflow.com
๐ŸŒ stackoverflow.com
php - On Duplicate Key Update - Multiple Columns - Stack Overflow
When using insert... on duplicate key update, what is the syntax to update multiple columns? INSERT INTO table1 (col1, col2, col3, col4) VALUES (โ€™$val1โ€™, โ€˜$val2โ€™, โ€˜$val3โ€™, โ€˜$val4โ€™) ON DUPLICATE KEY More on stackoverflow.com
๐ŸŒ stackoverflow.com
mysql - On Duplicate Key Update same as insert - Stack Overflow
I've searched around but didn't find if it's possible. I've this MySQL query: INSERT INTO table (id,a,b,c,d,e,f,g) VALUES (1,2,3,4,5,6,7,8) Field id has a "unique index", so there can't be two of... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Stack Exchange
dba.stackexchange.com โ€บ questions โ€บ 258914 โ€บ insert-on-duplicate-key-update-to-update-multiple-rows-in-single-query
mysql - INSERT ON DUPLICATE KEY UPDATE to UPDATE multiple rows in single query - Database Administrators Stack Exchange
February 4, 2020 - There is no clarification in regards to VALUES() if it is using the $row (e.g. from PHP) data to UPDATE if it differs from what is already in the row. There is no clarification in regards to if all the columns must be specified (besides the the primary or a unique key). The documentation at both MariaDB and MySQL leaves a nebulous mystical cloud of unknowns.
๐ŸŒ
Plus2Net
plus2net.com โ€บ sql_tutorial โ€บ sql_update-on-duplicate-key.php
Update multiple records by using ON DUPLICATE KEY UPDATE in MySQL
February 5, 2000 - INSERT INTO `student3` (`id`, `name`, `class`, `social`, `science`, `math`) VALUES (2, 'Max Ruin', 'Three', 86, 57, 86) on duplicate key update social=86,science=57,math=86 We will get a message saying 2 rows inserted, but actually we have updated one record only. Here MySQL will return the number of affected rows based on the action it performed.
๐ŸŒ
MySQL
bugs.mysql.com โ€บ bug.php
MySQL Bugs: #97027: Bulk insert with ON DUPLICATE KEY UPDATE breaks replicaiton
row *************************** ... set (0.00 sec) If you run the following statement the replication breaks: insert into t1 values(1, 'test'), (1, 'bar') on duplicate key update test=values(test); I created a testcase which is able to produce this behaviour....
๐ŸŒ
MySQL
dev.mysql.com โ€บ doc โ€บ refman โ€บ 8.0 โ€บ en โ€บ insert-on-duplicate.html
MySQL :: MySQL 8.0 Reference Manual :: 15.2.7.2 INSERT ... ON DUPLICATE KEY UPDATE Statement
January 13, 2022 - INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4,5,6) AS new ON DUPLICATE KEY UPDATE c = new.a+new.b; If, in addition, you use the column aliases m, n, and p, you can omit the row alias in the assignment clause and write the same statement like this: INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4,5,6) AS ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ mysql โ€บ mysql-insert-on-duplicate-update.htm
MySQL โˆ’ Insert on Duplicate Key Update
Following are the syntaxes of this ... new one in MySQL table through a PHP program, we use the DUPLICATE KEY UPDATE along with INSERT statement using the mysqli function query() as โˆ’...
๐ŸŒ
Reddit
reddit.com โ€บ r/mysql โ€บ how to run insert query with on duplicate key update in a single batch
r/mysql on Reddit: How to run INSERT query with ON DUPLICATE KEY UPDATE in a single batch
January 18, 2020 -

Hello,

I want to insert multiple rows in a single query, which have ON DUPLICATE KEY UPDATE statements for each row.

I want to do this for performance reasons, I can achieve this by running all insert queries in a loop from application code, but that'll be too heavy.

MySQL => 5.7

Application code =>Python 3.6

Find elsewhere
๐ŸŒ
MySQL
dev.mysql.com โ€บ doc โ€บ en โ€บ insert-on-duplicate.html
MySQL :: MySQL 8.4 Reference Manual :: 15.2.7.2 INSERT ... ON DUPLICATE KEY UPDATE Statement
INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4,5,6) AS new ON DUPLICATE KEY UPDATE c = new.a+new.b; If, in addition, you use the column aliases m, n, and p, you can omit the row alias in the assignment clause and write the same statement like this: INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4,5,6) AS ...
๐ŸŒ
Prisma
prisma.io โ€บ dataguide โ€บ mysql โ€บ inserting-and-modifying-data โ€บ insert-on-duplicate-key-update
ON DUPLICATE KEY UPDATE to upsert and modify data in MySQL
The ON DUPLICATE KEY UPDATE clause allows us to do this: INSERT INTO director (id, name) VALUES (3, 'susan') ... MySQL considers an ON DUPLICATE KEY UPDATE where an update occurs to the existing row as two rows affected.
๐ŸŒ
MySQL Tutorial
mysqltutorial.org โ€บ home โ€บ mysql basics โ€บ mysql insert on duplicate key update statement
MySQL INSERT ON DUPLICATE KEY UPDATE Statement
December 27, 2023 - INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...) AS new_data -- Row alias ON DUPLICATE KEY UPDATE column1 = new_data.column1, column2 = new_data.column2 + 1;Code language: SQL (Structured Query Language) (sql) MySQL also allows you to assign aliases to columns to avoid ambiguity, especially with long column names:
๐ŸŒ
Rip Tutorial
riptutorial.com โ€บ insert, on duplicate key update
MySQL Tutorial => INSERT, ON DUPLICATE KEY UPDATE
INSERT INTO `table_name` (`index_field`, `other_field_1`, `other_field_2`) VALUES ('index_value', 'insert_value', 'other_value') ON DUPLICATE KEY UPDATE `other_field_1` = 'update_value', `other_field_2` = VALUES(`other_field_2`);
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ mysql โ€บ insert-on-duplicate-key-update-in-mysql
MySQL INSERT ON DUPLICATE KEY UPDATE Statement - GeeksforGeeks
July 23, 2025 - When a duplicate value is entered in the primary key column, it raises an error. But the ON DUPLICATE KEY UPDATE clause handles this error by updating the row of the Primary key column. MySQL INSERT ON DUPLICATE KEY UPDATE statement syntax is:
Top answer
1 of 9
108

The UPDATE statement is given so that older fields can be updated to new value. If your older values are the same as your new ones, why would you need to update it in any case?

For eg. if your columns a to g are already set as 2 to 8; there would be no need to re-update it.

Alternatively, you can use:

INSERT INTO table (id,a,b,c,d,e,f,g)
VALUES (1,2,3,4,5,6,7,8) 
ON DUPLICATE KEY
    UPDATE a=a, b=b, c=c, d=d, e=e, f=f, g=g;

To get the id from LAST_INSERT_ID; you need to specify the backend app you're using for the same.

For LuaSQL, a conn:getlastautoid() fetches the value.

2 of 9
55

There is a MySQL specific extension to SQL that may be what you want - REPLACE INTO

However it does not work quite the same as 'ON DUPLICATE UPDATE'

  1. It deletes the old row that clashes with the new row and then inserts the new row. So long as you don't have a primary key on the table that would be fine, but if you do, then if any other table references that primary key

  2. You can't reference the values in the old rows so you can't do an equivalent of

    INSERT INTO mytable (id, a, b, c) values ( 1, 2, 3, 4) 
    ON DUPLICATE KEY UPDATE
    id=1, a=2, b=3, c=c + 1;
    

I'd like to use the work around to get the ID to!

That should work โ€” last_insert_id() should have the correct value so long as your primary key is auto-incrementing.

However as I said, if you actually use that primary key in other tables, REPLACE INTO probably won't be acceptable to you, as it deletes the old row that clashed via the unique key.

Someone else suggested before you can reduce some typing by doing:

INSERT INTO `tableName` (`a`,`b`,`c`) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE `a`=VALUES(`a`), `b`=VALUES(`b`), `c`=VALUES(`c`);
๐ŸŒ
Experts Exchange
experts-exchange.com โ€บ questions โ€บ 28221466 โ€บ PDO-MySQL-Prepared-multiple-row-insertion-with-ON-DUPLICATE-KEY-UPDATE.html
Solved: PDO MySQL Prepared multiple row insertion with ON DUPLICATE KEY UPDATE | Experts Exchange
August 23, 2013 - I may not be saving much per query by concatenating them, as one insert, but all those extra seconds add up when you're updating thousands of rows... So skull you are saying like this insert into table (column1,column2) values (:val1,:val2),(:val1,:val2 ยท ) on duplicate key update col1=VALUES(:val1),col2=VA
๐ŸŒ
MariaDB
mariadb.com โ€บ kb โ€บ en โ€บ insert-on-duplicate-key-update
INSERT ON DUPLICATE KEY UPDATE | Server | MariaDB Documentation
Complete guide to inserting data in MariaDB. Complete INSERT syntax for single rows, bulk operations, and ON DUPLICATE KEY handling for production use.
๐ŸŒ
PHP Freaks
forums.phpfreaks.com โ€บ sql / database โ€บ mysql help
INSERT ON DUPLICATE KEY UPDATE with Composite key - MySQL Help - PHP Freaks
December 23, 2016 - Why does the INSERT ON DUPLICATE KEY UPDATE fail? I read that I also need to add a UNIQUE INDEX (but I have no idea why) and did so, but it still fails. Thanks mysql> EXPLAIN points; +-------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-...