用SQL语句去掉重复的记录

删除手机(mobilePhone),电话(officePhone),邮件(email)同时都相同的数据

1.delete from 表 where id not in (select max(id) from 表 group by mobilePhone,officePhone,email )               or       delete from 表 where id not in (select min(id) from 表 group by mobilePhone,officePhone,email )

2.将没有重复的数据ID选出来放在临时表里,再将表的信息按临时表的选择出来的ID,将它们找出来插入到新的表,然后将原表删除

//查询出唯一数据的ID,并把他们导入临时表tmp中      

 select min(id) as mid into tmp from 表 group by mobilePhone,officePhone,email  
//查询出去重后的数据并插入finally表中    
insert into finally select (除ID以外的字段) from customers_1 where id in (select mid from tmp)   
查找表中存在这几个字段的重复数据并按照插入的时间先后进行删除
  select row_num = row_number() over(partition by 字段,字段 order by 时间 desc)  
   from 表 where 时间> getdate()-1  
   ) tmp   
  where row_num > 1   
select max(主键ID) from 表 group by 需要去重的字段 having count(需要去重的字段)>=1  
 )   
搜索过滤重复数据
1.select distinct name from A
 
posted @ 2019-12-09 14:18  1班李帅伟  阅读(530)  评论(0编辑  收藏  举报