INSERT ... ON DUPLICATE KEY UPDATE

This statement is based on a unique index or primary key. For example, a unique index is added to a field a, and there is already a record in the table with a value of 1. The following two statements will have the same effect:

 

INSERT INTO table (a,b,c) VALUES (1,2,3)

  ON DUPLICATE KEY UPDATE c=c+1;

 

 

UPDATE table SET c=c+1 WHERE a=1;

 

 

Multiple fields can be placed after ON DUPLICATE KEY UPDATE, separated by commas. Using ON DUPLICATE KEY UPDATE, finally if a new row is inserted, the number of rows affected is 1, if an existing row of data is modified, the number of rows affected is 2, and if the value does not change, the number of rows affected is The number is 0.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326112598&siteId=291194637