mysql多表多字段关联删除

本文介绍了一种在MySQL中高效删除重复数据的方法。通过创建一个包含唯一标识符的临时表,并利用该表来删除原始表中的冗余记录,从而达到去重的目的。此方法能够减少查询行数并提高处理效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

eg:

delete t1 from table1 t1,  table2 t2 
where   
 t1.fkcustomerid = t2.fkcustomerid and t1.fkpersonid = t2.fkpersonid and t1.fcooperationtype = t2.fcooperationtype  and 
t1.fid not in (
select min_fid from table3 t3 


这样可以读到索引  ,可以用explain去分析,会比in要少查询很多行,mysql里面In里面接子查询 读不到索引



删除表里面的重复数据demo:


 -- 1.先把去重的数据备份好  用于后面删除的关联
create table t_bc_belongperson_170907notrepeat as 
select min(fid) as minid from t_bc_belongperson bp 
where bp.fcooperationtype in ('PRIVATE','MANAGER_GIVE','PUBTOPRI') 
group by fkcustomerid ;

-- 2.创建索引 
alter table t_bc_belongperson_170907notrepeat add index idx_07_miniid(minid);

-- 3.删除之前先备份好客户归属人的数据 
create table t_bc_belongperson170907bak as select * from t_bc_belongperson;

-- 4.执行删除重复数据 
delete from t_bc_belongperson where fid not in (
select minid from t_bc_belongperson_170907notrepeat
); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值