For example, if column a is defined as unique and the value is 1, the following statements have the same effect, that is, once the entry and exit records contain a = 1, directly update c = c + 1 without executing c = 3.
Copy codeThe Code is as follows:
Insert into table (a, B, c) values (1, 2, 3) on duplicate key
Update c = c + 1; 1 update table set c = c + 1 where a = 1;
It is also worth mentioning that this statement is in mysql, but not in standard SQL statements.
Insert into .. on duplicate key to update multiple rows
If on duplicate key update is specified at the end of the INSERT statement, and DUPLICATE values appear in a UNIQUE index or primary key after the row is inserted, the old row UPDATE is executed; if the unique value column is not duplicate, a new row is inserted. For example, if column a is defined as UNIQUE and contains a value of 1, the following two statements have the same effect:
Copy codeThe Code is as follows:
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;
If a row is inserted as a new record, the value of the affected row is 1. If the original record is updated, the value of the affected row is 2.
If you want to know