首页 > 代码库 > mysql 查找除id外其他重复的字段数据

mysql 查找除id外其他重复的字段数据

如表 test1 有多个重复的字段

技术分享

其中有些数据完全重复是错误的数据,我们要把他找出来,然后删除掉

select * from test1 a where (a.phone,a.name) in (

select phone,name from test1 group by phone,name having count(*)>1

) and id not in (

select max(id) from test1 group by phone,name having count(*)>1

);

结果

技术分享

然后就可以用php或其他语言来删除这些 id 了

或者 

把前面的那个select换成

delete 

delete from test1 where (a.phone,a.namein (

select phone,name from test1 group by phone,name having count(*)>1

) and id not in (

select max(id) from test1 group by phone,name having count(*)>1

);

mysql 查找除id外其他重复的字段数据