If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL performs an UPDATE of the old row. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have similar effect:
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; UPDATE table SET c=c+1 WHERE a=1;
如果在INSERT
中指定ON DUPLICATE KEY UPDATE
,并且插入将在UNIQUE
索引或PRIMARY KEY
中导致重复值的行,MySQL
执行旧行的更新。比如,如果列a被声明为UNIQUE
并且包含值1,以下两句话有相同的效果。