由于并发,导致数据库中同样的数据多了几份,所以需要清楚数据。比如去除person表中重名的数据(实际中判断行数据相同可能通过多个字段判断,这里只是通过name来确定)
一、将名字为张三数据去重;
delete from person where name='张三' and id not in (select max(id) id from person where name='张三' group by name having count(name) > 1)
1、先查询出姓名为张三的所有数据,然后分组,分组的数据大于1的说明有重复的,这个根据需要保留max或者min那条数据随意选择;
2、在相同的条件下,删除重复的数据排除max或min那条;
二、全表去重;
delete from person where id not in (select max(id) id from person group by name having count(name) > 1)