Requirements : Update if it exists, and add it if it does not exist.
Method : It can be easily implemented with kv of redis, and there is such a function in MySQL: ON DUPLICATE KEY UPDATE in INSERT
Conditions of use : where a, b, and c fields must have at least one UNIQUE index or PRIMARY KEY in order to determine record uniqueness
语句:
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=4
If the 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
or
You can use the VALUES(col_name) function in the UPDATE clause to reference column values from the INSERT part of the INSERT...UPDATE statement. The VALUES() function is only meaningful in INSERT...UPDATE statements, otherwise it will return NULL
Example:
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(b) + VALUES(c)