Mysql inserts data in batches, duplicate data is not inserted or directly updated

When inserting data in batches, the data cannot be inserted due to primary key conflicts, how to solve it

1.on DUPLICATE key

For existing data, update the updatetime, add on DUPLICATE key after the sql statement, then the existing data will be updated directly

For example:

INSERT INTO `user_center`.`tt_tenantry_user`(`tenant_id`, `user_id`, `update_time`) VALUES ('T_DW', '001', now()) 
on DUPLICATE key  update update_time=now();
INSERT INTO `user_center`.`tt_tenantry_user`(`tenant_id`, `user_id`,`update_time`) VALUES ('T_DW', '002', now()) 
on DUPLICATE key  update update_time=now();

2.insert ignore into

When inserting data, such as duplicate data, no error will be returned, only a warning will be returned

For example: 

insert ignore into  `user_center`.`tt_tenantry_user`(`tenant_id`, `user_id`, `update_time`) 
VALUES ('T_DW', '001', now()) ;

Guess you like

Origin blog.csdn.net/bugua3542/article/details/121704563