mysql - UPDATE record if present; else INSERT -


I want to update a record that is present in a table or not. If it is not present in the database then it will be inserted.

To prevent the selection, I am using the first UPDATE statement and affected_rows & gt; 0 If not, then I am inserting this record into the table.

I was thinking that there is a better way to do this?

You can use the duplicate key update on INSERT ... syntax :

  Insert in the table (A, B, C) duplicate key update at values ​​(1,2,3) c = C + 1;   


The difference between this and REPLACE (Femaref's answer) is that REPLACE If a key is duplicated, delete its old line and then delete , while the update existing row

div>

Comments