20 March 2014

MySQL: Insert... on Duplicate Key Update

Continue from Insert Ignore.
Insert ignore check based on unique or primary key.
We need syntax to provide this scenario:
If data is not exist then insert, else if already exist we'll just update it.

 We can use this mysql syntax :

Single record:
INSERT INTO table (pkfield,a,b) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE a=2, b=3;


Multiple Record:
INSERT INTO table (pkfield,a,b) VALUES (1,2,3),(4,5,6)
  ON DUPLICATE KEY UPDATE a=VALUES(a), b=VALUES(b);

Happy Coding everyone...




Click here if you like this article.


0 comments: