Duplicate Key Update will not be updated, but generates an error for duplicates

I have a table with a primary key with two columns (key1,key2).

This is my request.

INSERT INTO mytable (key1, key2, val1, val2) 
VALUES (:k1, :k2, :v1, :v2)
ON DUPLICATE KEY UPDATE val1 = val1 + :v1, val2 = val2 + :v2

Now, when I insert a row with a duplicate key, I get this error message:

Disable a PDOException with the message "SQLSTATE [23000]: Integrity constraint violation: 1062 Duplicate entry '157-433' for key 'PRIMARY' in ...

Why doesn't he just update the existing (157,433) -row?

+4
source share
1 answer

. MySQL - VALUES(colname) ON DUPLICATE KEY, , , . :

INSERT INTO mytable (key1, key2, val1, val2)
VALUES (:k1, :k2, :v1, :v2)
ON DUPLICATE KEY UPDATE val1 = val1 + VALUES(val1), val2 = val2 + VALUES(val2)

, .

+8

Source: https://habr.com/ru/post/1546474/


All Articles