Today do inference insert used in MySQL on DUPLICATE KEY UPDATE, now Mark below!
If you want to be able to insert data and update data without data in the database, you can choose on DUPLICATE KEY update.
On DUPLICATE key update can run 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 includes 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 (if 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 running, 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 running, the value of C changes to 7 (the second is repeated with the first one, and C is the value 7 of the direct fetch).
Note:on DUPLICATE KEY update is only a unique syntax for MySQL, not SQL standard syntax!
Official document Photo: Dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#insert