3

mysql批量插入时,如何不插入重复数据

 3 years ago
source link: https://segmentfault.com/a/1190000039824833
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

mysql批量插入时,如何不插入重复数据

发布于 今天 13:27

1.insert ignore into
example:insert ignore into user (id,name) value(1,'lisi');
作用:当插入数据存在相同的primary或unique时,此条数据不会执行并忽略报错。 执行下一条数据
这中做法不会因为报错而终止程序执行。但是要注意,也会忽略由于其他原因造成的报错。

2.on duplicate key update
example:insert into user (id,name) values(1,'lisi') on duplicate key update id = id;
当priamry或unique重复时执行update语句,如果不重复则直接插入数据。但错误不会被忽略

3.replace into
example:replace into user (id,name) values(1,'lisi');
无论是否存在相同的数据,都会先删除再插入


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK