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`);
🌐
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.
🌐
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
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 - With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT_FOUND_ROWS flag to the mysql_real_connect() C API function when connecting to mysqld, the affected-rows value is 1 (not 0) if an existing row is set to its current values.
🌐
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 −
🌐
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:
Find elsewhere
🌐
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).
🌐
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:
🌐
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....
🌐
MySQL
dev.mysql.com › doc › en › insert.html
MySQL :: MySQL 8.4 Reference Manual :: 15.2.7 INSERT Statement
SELECT form inserts rows selected from another table or tables. You can also use INSERT ... TABLE to insert rows from a single 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 ...
🌐
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 where "INSERT ON DUPLICATE UPDATE" comes to the rescue. With this feature, you can achieve your goal in a single SQL statement. 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';
🌐
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'
🌐
Database Guide
database.guide › using-insert-on-duplicate-key-update-in-mysql
Using INSERT … ON DUPLICATE KEY UPDATE in MySQL
Now, we’ll attempt to insert a new row with the same ProductName (Laptop). Since ProductName is a unique key, this will trigger an update: INSERT INTO Products (ProductName, Quantity, Price) VALUES ('Laptop', 15, 899.99) ON DUPLICATE KEY UPDATE Quantity = Quantity + 15, Price = 899.99;
🌐
NAO IT Systems LLC.
digibeatrix.com › home › mysql › sql basics › mysql on duplicate key update explained: syntax, examples, best practices
MySQL ON DUPLICATE KEY UPDATE Explained: Syntax, Examples, Best Practices - Practical MySQL & MariaDB
February 7, 2026 - Best Practices: Use transactions and error handling to process data safely and efficiently. MySQL’s ON DUPLICATE KEY UPDATE is a powerful feature that enables efficient data handling.