For smart Primates & ROBOTS (oh and ALIENS ).

Tuesday, November 17, 2015

How to use ON DUPLICATE KEY in insert statement

How to use ON DUPLICATE KEY in insert statement

Problem:
In case of inserting or updating record we use separate logic using if statement or case statement:
e.g.:

if(i==0)
insert here

if(i=1)
update here

we can terminate the above if statement by using On Duplicate Key.
by using this only one statement can be use for insert and update record.

*note: user_nm coulmn must be unique

Example:

INSERT INTO `posts` (`user_nm`, `no_of_update`, `inserted_on`, `update_on`)
VALUES ('a_user', 1, NOW(), NOW())
ON DUPLICATE KEY
UPDATE `no_of_update` = `no_of_update` + 1, `update_on` = NOW()



















Share:

Featured Post

Core Web Vitals Assessment showing LCP 10.6s while google pagespeed report showing LCP 3.5s why?

Core Web Vitals Assessment showing LCP 10.6s while google pagespeed report showing LCP 3.5s why? So, you can face this situation while...

Ads Inside Post

Powered by Blogger.

Arsip