INSERT INTO tb (name, date, stat1, stat2, stat3)
VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE stat1 = stat1 + VALUES(stat1), stat2 = stat2 + VALUES(stat2), stat3 = stat3 + VALUES(stat3)
Answer from vearutop on Stack Overflow
๐ŸŒ
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 basic syntax for the insert or update operation looks like this: ... Multiple columns can be provided after the ON DUPLICATE KEY UPDATE clause, each defining what the new value should be if there's a conflict with an existing record.
Discussions

php - On Duplicate Key Update - Multiple Columns - Stack Overflow
When using insert... on duplicate ... update multiple columns? INSERT INTO table1 (col1, col2, col3, col4) VALUES (โ€™$val1โ€™, โ€˜$val2โ€™, โ€˜$val3โ€™, โ€˜$val4โ€™) ON DUPLICATE KEY UPDATE col2=โ€˜$val2โ€™, col3=โ€˜$val3โ€™, col4=โ€˜$val4โ€™ // <-- not sure ยท Update: I am using this within PHP. Since this is a syntax question, it very relevant. $result = mysql_query("INSERT ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
MySQL behavior of ON DUPLICATE KEY UPDATE for multiple UNIQUE fields - Stack Overflow
Is there any way of having a unique constraint on the 2nd column, but have it so it does not affect this duplicate key update operation? This means the insert or update fails if the unique constraint is not met. 2015-07-25T12:24:54.333Z+00:00 ... My tests contradict what this answer claims (on MySQL ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
mysql - "ON DUPLICATE KEY UPDATE" is not working for multiple columns - Stack Overflow
Executing the query for the first time is working fine for inserting multiple column records but ON DUPLICATE KEY UPDATE is not working for the same records if that query is executed again. It inse... More on stackoverflow.com
๐ŸŒ stackoverflow.com
sql - MySQL ON DUPLICATE KEY UPDATE for multiple rows insert in single query - Stack Overflow
Great suggestion. If you want to increment a value when duplicate, add this after the "ON DUPLICATE KEY UPDATE" part: total = total + VALUES( total ) 2020-06-01T19:14:06.757Z+00:00 ... Caution: The use of VALUES() to refer to the new row and columns is deprecated beginning with MySQL 8.0.20. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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 - If a table contains an AUTO_INCREMENT column and INSERT ... ON DUPLICATE KEY UPDATE inserts or updates a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.
๐ŸŒ
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
If a table contains an AUTO_INCREMENT column and INSERT ... ON DUPLICATE KEY UPDATE inserts or updates a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.
๐ŸŒ
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 - By using the IF() function within the ON DUPLICATE KEY UPDATE clause, I can ensure that once the textCode column is "suppressed", any subsequent execution of this query will become a no-op.
๐ŸŒ
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 - If a table contains an AUTO_INCREMENT column and INSERT ... ON DUPLICATE KEY UPDATE inserts or updates a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.
Find elsewhere
๐ŸŒ
MySQL
dev.mysql.com โ€บ doc โ€บ refman โ€บ 5.7 โ€บ en โ€บ insert-on-duplicate.html
MySQL :: MySQL 5.7 Reference Manual :: 13.2.5.2 INSERT ... ON DUPLICATE KEY UPDATE Statement
January 22, 2020 - If a table contains an AUTO_INCREMENT column and INSERT ... ON DUPLICATE KEY UPDATE inserts or updates a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.
๐ŸŒ
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:
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ mysql โ€บ mysql-insert-on-duplicate-update.htm
MySQL โˆ’ Insert on Duplicate Key Update
To update a duplicate row with ... we use the DUPLICATE KEY UPDATE along with INSERT statement using the mysqli function query() as โˆ’ ยท $sql = "INSERT INTO my_table (column1, column2, ...) VALUES (value1, value2), (value3, value4), ......
๐ŸŒ
MySQL
bugs.mysql.com โ€บ bug.php
MySQL Bugs: #73925: INSERT INTO ... ON DUPLICATE KEY UPDATE not taking into account multiple column
September 16, 2014 - Sorry, I checked the constraints on the table - I had multiple ones. After deleting them and keeping only the PRIMARY KEY for the multiple intended columns, everything works as expected.
๐ŸŒ
Josephscott
blog.josephscott.org โ€บ 2011 โ€บ 02 โ€บ 28 โ€บ mysql-insert-on-duplicate-and-multiple-rows
MySQL: INSERT ON DUPLICATE and Multiple Rows โ€“ Joseph Scott
Without ON DUPLICATE youโ€™d generally ... insert and ON DUPLICATE features to do: [sourcecode lang=โ€sqlโ€] INSERT INTO table_name (a,b,c) VALUES (1,2,3), (4,5,6), (7,8,9) ON DUPLICATE KEY UPDATE c = c + 1; [/sourcecode]...
Top answer
1 of 2
39

Consider

INSERT INTO table (a,b,c) VALUES (1,2,3)
    -> ON DUPLICATE KEY UPDATE c=c+1;

If a and b are UNIQUE fields, UPDATE occurs on a = 1 OR b = 2. Also when condition a = 1 OR b = 2 is met by two or more entries, update is done only once.

Ex here table table with Id and Name UNIQUE fields

Id     Name     Value 
1      P        2 
2      C        3 
3      D        29 
4      A        6

If query is

INSERT INTO table (Id, Name, Value)
VALUES (1, C, 7);

then we get

Id     Name     Value 
1      P        2 
2      C        3 
3      D        29 
4      A        6
1      C        7

which violates uniqueness of Id and Name. Now with

INSERT INTO table (Id, Name, Value)
VALUES (1, C, 7)
ON DUPLICATE KEY UPDATE Value = 7;

we get

Id     Name     Value 
1      P        7 
2      C        7 
3      D        29 
4      A        6

Behavior on multiple keys is the following

UPDATE in ON DUPLICATE KEY UPDATE is performed if one of the UNIQUE field equals the value to be inserted. Here, UPDATE is performed on Id = 1 OR Name = C. It is equivalent to

UPDATE table 
SET Value = 7
WHERE Id = 1 OR Name = C;

What if I want one update only, for either key

Can use UPDATE statement with LIMIT keyword

UPDATE table 
SET Value = 7
WHERE Id = 1 OR Name = C
LIMIT 1;

which will give

Id     Name     Value 
1      P        7 
2      C        3 
3      D        29 
4      A        6

What if I want one update only if values for both keys are matched

One solution is to ALTER TABLE and make the PRIMARY KEY (or uniqueness) work on both fields.

ALTER TABLE table 
DROP PRIMARY KEY,
ADD PRIMARY KEY (Id, Name);

Now, on

INSERT INTO table (Id, Name, Value)
VALUES (1, C, 7)
ON DUPLICATE KEY UPDATE Value = 7;

we get

Id     Name     Value 
1      P        2 
2      C        3 
3      D        29 
4      A        6
1      C        7

since no duplicate (on both keys) is found.

2 of 2
0
  1. how does MySQL behave ... It behaves as expected, that is executes ON DUPLICATE KEY clause.

  2. Can I have one update for either... In reality, you have only one ON DUPLICATE KEY clause, so you need to put some code to differentiate which constraint was involved. Fortunatelly, it is possible. The only thing you should know, the order of assignment matters, and you can assign multiple times. Suppose, you have unique constraint on a and b, and you want to update c only if a uniqueness is involved: ... KEY UPDATE c = IF(a = VALUES(a) and b <> VALUES(b), VALUES(c), c), b = VALUES(b)

    but if you change the order of assignments the second condition within if will be always false.

  3. See 2.

๐ŸŒ
Javatpoint
javatpoint.com โ€บ mysql-insert-on-duplicate-key-update
MySQL Insert On Duplicate Key Update - javatpoint
MySQL Insert On Duplicate Key Update with mysql tutorial, examples, functions, programming, mysql, literals, cursor, procedure, regexp_like(), regexp_replace operator, regular expression, crud etc.
๐ŸŒ
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.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 54742887
mysql - "ON DUPLICATE KEY UPDATE" is not working for multiple columns - Stack Overflow
INSERT INTO info(id, docid, deptid, catid, name) VALUES (2,5,2,2,'John Adison') ON DUPLICATE KEY UPDATE docid = concat(docid,',',5), deptid = concat(deptid,',',2), catid = concat(catid,',',2); the output should be unchanged if it gets same records ...
๐ŸŒ
Black Sail Division
blacksaildivision.com โ€บ mysql-on-duplicate-key
MySQL insert or update using ON DUPLICATE KEY UPDATE - Black Sail Division
November 22, 2013 - For instance we want to update our record only if another passed column has different value or something. Let's take a look at this example: INSERT INTO stats (article_id, created) VALUES (12, CURRENT_DATE()) ON DUPLICATE KEY UPDATE views_count = IF(VALUES(article_id) = 12, views_count + 2, views_count + 1) /*, additional_field = additional_field + 1, next_field = IF(article_id = 1, 1, 0)
๐ŸŒ
MariaDB
mariadb.com โ€บ kb โ€บ en โ€บ insert-on-duplicate-key-update
INSERT ON DUPLICATE KEY UPDATE | Server | MariaDB Documentation
INSERT INTO ins_duplicate VALUES (4,'Gorilla') ON DUPLICATE KEY UPDATE animal='Gorilla'; Query OK, 1 row affected (0.07 sec) ... SELECT * FROM ins_duplicate; +----+----------+ | id | animal | +----+----------+ | 1 | Aardvark | | 2 | Cheetah | | 3 | Zebra | | 4 | Gorilla | +----+----------+ ... INSERT INTO ins_duplicate VALUES (1,'Antelope'); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'