This is one of the great feature available in MySQL from version 4.1, but most of us forgotten this feature or reluctant to use.
This feature avoids sending an extra "select" query before inserting a row.

How it works:

When you specify "on duplicate key update" in your query, in case  it causes a duplicate value on primary / unique key column, update  portion of that query will be executed. You can also able to update multiple columns on duplicate values, only thing is we need to separate it by commas.

Example:


INSERT INTO table(ip,views,last_visit) values('122.168.221.25',1,CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE views=views+1,last_visit=CURRENT_TIMESTAMP;

 


Comments (0)
Leave a Comment

loader Posting your comment...