insert or update sql code example
Example 1: mysql insert on dupèlicate
INSERT INTO table_name(c1)
VALUES(c1)
ON DUPLICATE KEY UPDATE c1 = VALUES(c1) + 1;
Example 2: sql insert or update
MERGE INTO table1 t1
USING table2 t2
ON (t1.CODE = t2.ID)
WHEN MATCHED THEN
UPDATE SET t1.COL1 = t2.VALUE1
WHEN NOT MATCHED THEN
INSERT (CODE, COL1) VALUES (t2.ID, t2.VALUE1);
REPLACE INTO table1 (pk_id, col1) VALUES (5, 'aaaa');
INSERT INTO table1 VALUES (key, generation)
ON DUPLICATE KEY UPDATE (key = key, generation = generation + 1);