Today make a judgment insert used in MySQL on DUPLICATE KEY UPDATE, now Mark below!
You can choose on DUPLICATE KEY Update if you want to insert the data and update the data if you do not have the data in the database.
The on DUPLICATE key update can perform an update operation on the old row in the presence of a unique index or primary key.
For example: If column A is defined as unique and contains a value of 1, the following two statements have the same effect:
INSERT into table (a,b,c) VALUES (All-in-all) on DUPLICATE KEY UPDATE C = c + 1,b = b-1;
UPDATE table SET c = c + 1,b = b-1 WHERE a = 1;
For example, if you insert multiple rows of records (assuming A is a primary key or A is a unique index column):
INSERT into TABLE (a,c) VALUES (1,3), (1,7) on DUPLICATE KEY UPDATE C = c + 1;
After execution, the value of C becomes 4 (the second is repeated with the first one, and C is +1 on the original value).
INSERT into TABLE (a,c) values (1,3), (1,7) on DUPLICATE KEY UPDATE C = values (c);
After execution, the value of C changes to 7 (the second is repeated with the first one, and C is the value of the duplicate 7 directly).
Note:on DUPLICATE KEY update is only a unique syntax for MySQL, not SQL standard syntax!
Official Document reference: Dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#insert