Return to Snippet

Revision: 72244
at June 14, 2017 06:21 by apphp-snippets


Initial Code
-- Unique key for table must be pre-defined
INSERT INTO `tableName`
VALUES (field1,field2,field3,'-1',...,fieldN,'0')
ON DUPLICATE KEY
UPDATE `field1` = 'abc', `field2` = '123'

Initial URL
http://www.apphp.com/index.php?snippet=mysql-insert-or-update-if-unique-key-already-exists

Initial Description
This snippet allows you to insert new record into table. In case a unique key already exists - it updates appropriate fields only, leaving other fields untouched.

Initial Title
Insert or Update Record if Unique Key Already Exists

Initial Tags
mysql

Initial Language
SQL