create table test1 (
id int not null auto_increment primary key,
a varchar(16), b varchar(16)
);
INSERT INTO test1 (a,b) VALUES ('a1','b3') ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), b='3';
The above line should insert a record because the table is empty.
INSERT INTO test1 (a,b) VALUES ('a1','b3') ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), b='3';
Run the line again, it should replace bwith "3", since it a1, b3already exists. But mysql adds another line for me. I searched for a while and cannot find a solution.
Last update: Thanks for your help. I decided that one of the columns should be unique.
Modify table test1 add unique (a)
solve the problem of.
source
share