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.

Answer from hjpotter92 on Stack Overflow
๐ŸŒ
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
In assignment value expressions in the ON DUPLICATE KEY UPDATE clause, you can use the VALUES(col_name) function to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE statement. In other words, VALUES(col_name) in the ON DUPLICATE KEY UPDATE clause refers to the value of col_name that would be inserted, had no duplicate-key conflict occurred.
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`);
๐ŸŒ
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.
๐ŸŒ
MariaDB
mariadb.com โ€บ docs โ€บ server โ€บ reference โ€บ sql-statements โ€บ data-manipulation โ€บ inserting-loading-data โ€บ insert-on-duplicate-key-update
INSERT ON DUPLICATE KEY UPDATE | Server | MariaDB Documentation
January 28, 2026 - INSERT ... ON DUPLICATE KEY UPDATE (often called "upsert") is a MariaDB/MySQL extension to the INSERT statement that, if it finds a duplicate unique or primary key, will instead perform an UPDATE.
๐ŸŒ
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 - In assignment value expressions in the ON DUPLICATE KEY UPDATE clause, you can use the VALUES(col_name) function to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE statement. In other words, VALUES(col_name) in the ON DUPLICATE KEY UPDATE clause refers to the value of col_name that would be inserted, had no duplicate-key conflict occurred.
๐ŸŒ
Oracle
docs.oracle.com โ€บ cd โ€บ E17952_01 โ€บ mysql-5.7-en โ€บ insert-on-duplicate.html
13.2.5.2 INSERT ... ON DUPLICATE KEY UPDATE Statement
February 9, 2026 - Now we attempt to insert two rows, one of which contains a duplicate key value, using ON DUPLICATE KEY UPDATE, where the UPDATE clause itself results in a duplicate key value: mysql> INSERT INTO t VALUES (2,3), (3,3) ON DUPLICATE KEY UPDATE a=a+1, b=b-1; ERROR 1062 (23000): Duplicate entry '1' for key 't.b' mysql> SELECT * FROM t; +---+---+ | a | b | +---+---+ | 1 | 1 | | 2 | 2 | +---+---+ 2 rows in set (0.00 sec)
๐ŸŒ
Bennadel
bennadel.com โ€บ blog โ€บ 4590-conditionally-updating-columns-when-using-on-duplicate-key-update-in-mysql.htm
Conditionally Updating Columns When Using ON DUPLICATE KEY UPDATE In MySQL
February 1, 2024 - In this SQL statement, I'm using the ON DUPLICATE KEY UPDATE clause in order to update any existing row with a matching email address (which, remember, is being used as the primary key).
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ mysql โ€บ mysql-insert-on-duplicate-update.htm
MySQL โˆ’ Insert on Duplicate Key Update
sql = "INSERT INTO my_table (column1, column2, ...) VALUES (value1, value2), (value3, value4), ... ON DUPLICATE KEY UPDATE column1 = value1, column2 = value2, ..."; con.query(sql); To update the duplicate row with new one in MySQL table through a Java program, we use the DUPLICATE KEY UPDATE along with INSERT statement using the JDBC function executeUpdate() as โˆ’
Find elsewhere
๐ŸŒ
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:
๐ŸŒ
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:
๐ŸŒ
MySQL
dev.mysql.com โ€บ doc โ€บ en โ€บ insert.html
MySQL :: MySQL 8.4 Reference Manual :: 15.2.7 INSERT Statement
SELECT form inserts rows selected ... table. INSERT with an ON DUPLICATE KEY UPDATE clause enables existing rows to be updated if a row to be inserted would cause a duplicate value in a UNIQUE index or PRIMARY KEY....
๐ŸŒ
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`, ... (10, 'Big John', 'Four', 56, 44, 56) ON DUPLICATE KEY UPDATE social=values(social),science=values(science),math=values(math); Above query will update 9 records with new data....
๐ŸŒ
Medium
medium.com โ€บ terales-engineering โ€บ mysql-on-duplicate-key-update-with-unique-index-and-pdo-support-410ee63b4463
MySQL `on duplicate key update` with unique index and PDO support | by Alexander Terehov | terales engineering | Medium
June 19, 2017 - INSERT INTO users (`uuid`, `name`, `image_url`) VALUES(:uuid, :name, :image_url) ON DUPLICATE KEY UPDATE `name` = :name, `image_url` = :image_url;-- Assuming that `users` table has these keys ALTER TABLE `users` ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `uuid` (`uuid`); Iโ€™ve found that from careful reading MySQL manual in 13.2.5.3 INSERT โ€ฆ
๐ŸŒ
jOOQ
jooq.org โ€บ doc โ€บ latest โ€บ manual โ€บ sql-building โ€บ sql-statements โ€บ insert-statement โ€บ insert-on-duplicate-key
The ON DUPLICATE KEY UPDATE clause of the INSERT statement
MERGE INTO AUTHOR USING ( SELECT 3, 'X' FROM (VALUES (1)) AS dual (dual) ) t (ID, LAST_NAME) ON AUTHOR.ID = t.ID WHEN MATCHED THEN UPDATE SET AUTHOR.LAST_NAME = 'X' WHEN NOT MATCHED THEN INSERT (ID, LAST_NAME) VALUES ( t.ID, t.LAST_NAME ) INSERT INTO AUTHOR (ID, LAST_NAME) VALUES ( 3, 'X' ) AS excluded ON DUPLICATE KEY UPDATE AUTHOR.LAST_NAME = 'X'
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ understanding-mysql-insert-duplicate-update-upsert-bilal-usean
Understanding MySQL INSERT ON DUPLICATE UPDATE OR UPSERT
September 15, 2023 - Here's the basic syntax: INSERT INTO employees (employee_id, name, department) VALUES (123, 'John Doe', 'Marketing') ON DUPLICATE KEY UPDATE name = 'John Doe', department = 'Marketing';