UPDATE if present; else INSERT

I want to update a record, which may or may not be in the table. If it is not in the database, it will be inserted.

To prevent selection, I first use the operator UPDATEand check affected_rows > 0if not, then I insert this record into the table.

I was wondering if there is a better way to do this?

+3
source share
2 answers

You can use the syntax INSERT ... ON DUPLICATE KEY UPDATE:

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=c+1;

http://dev.mysql.com/doc/refman/4.1/en/insert-on-duplicate.html


REPLACE ( Femaref) , REPLACE , , , .

+5

Source: https://habr.com/ru/post/1766588/


All Articles