If the on duplicate key update is specified at the end of the INSERT statement and the DUPLICATE value appears in a UNIQUE index or primary key after the row is inserted, UPDATE is executed ON the row with the DUPLICATE value;
If the unique value column is not duplicate, a new row is inserted. Www.2cto.com
For example, if column a is a primary key or has a UNIQUE index with a value of 1, the following two statements have the same effect: 1 insert into table (a, c) VALUES) on duplicate key update c = c + 1; 2 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. This syntax can also be used as follows:
If you INSERT multiple rows of records (assuming a is the primary key or a is a UNIQUE index column): 11. insert into table (a, c) VALUES (1, 3), (1, 7) on duplicate key update c = c + 1; after execution, the value of c is changed to 4 (the second repeat with the first one, and c is on the original value + 1 ). 1 www.2cto.com 2. insert into table (a, c) VALUES (1, 3), (1, 7) on duplicate key update c = VALUES (c );
After the execution, the value of c will change to 7 (the second repeat with the first repeat, and c will take the repeat value 7 directly ).
Note: on duplicate key update is only a special MySQL syntax, not a standard SQL syntax! This syntax and applicable in the need to determine whether the record exists, if there is no insert exists then update the scene. can refer to the Syntax: http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#insert author Snowman