Posted By

apphp-snippets on 06/14/17


Tagged

mysql key unique


Versions (?)

Insert or Update Record if Unique Key Already Exists


 / Published in: SQL
 

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

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.

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

Report this snippet  

You need to login to post a comment.