Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save apphp/aa9ae09966b3dac277da70a8e8548fca to your computer and use it in GitHub Desktop.
Save apphp/aa9ae09966b3dac277da70a8e8548fca to your computer and use it in GitHub Desktop.
Insert or Update Record if Unique Key Already Exists
-- source: http://www.apphp.com/index.php?snippet=mysql-insert-or-update-if-unique-key-already-exists
-- 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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment