MySql删除表中重复数据

有一表中存在大量重复数据 在此记录下我删除表内重复数据的方法

-- 新增测试表
create table basic_farmer
(
id INT(11),
user_name VARCHAR(25),
id_card INT (11)
);

以下以单个字段不能重复测试 

1 首先查询有哪些重复数据 

-- 查询有哪些数据重复(id_card不能重复)
select id_card from basic_farmer 
group by id_card having count(id_card) > 1


-- 查全表中哪些数据重复
select user_name,id_card, id from basic_farmer 
where id_card in 
(select id_card from basic_farmer 
group by id_card having count(id_card) > 1)
ORDER BY user_name;

MySql删除表中重复数据_第1张图片

 

2 查询重复数据的id

-- 查询要删除的id 
select id from 
(select a.id from basic_farmer a left join (
select id_card,min(id) id from basic_farmer 
group by id_card having count(id_card) > 1) b
on a.id_card = b.id_card where a.id != b.id ) as tt

min(id)保留 id较小的一条数据  max(id) 保留id较大的一条数据 

3 删除重复数据

如果这样写会报错

You can't specify target table 'basic_farmer' for update in FROM clause

-- 错误写法
DELETE from basic_farmer where id in (
select a.id from basic_farmer a left join (
select id_card,min(id) id from basic_farmer 
group by id_card having count(id_card) > 1) b
on a.id_card = b.id_card where a.id != b.id)

MySql删除表中重复数据_第2张图片

原因:更新这个表的同时又查询了这个表 可以理解为死锁 mysql不支持这种更新查询同一张表的操作

解决方法:把更新的数据查询出来做一个临时表 然后删除

-- 删除重复数据
delete from basic_farmer where id in (
select id from (select a.id from basic_farmer a 
left join (
select id_card,min(id) id from basic_farmer 
group by id_card having count(id_card) > 1) b
on a.id_card = b.id_card where a.id != b.id ) as tt);

where id in中sql做临时表  防止更新这个表的同时又查询了这个表


最后MySql删除表中重复数据就讲完了 大家根据表和字段 替换sql即可(删除前 表记得备份哦!)

欢迎大家在评论区讨论 有什么问题也可以私信我哦

你可能感兴趣的:(开发合集,MySql,1024程序员节,java,mysql,sql,数据库)