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

);
posted @ 2017-06-01 11:56  freedom/cn  阅读(3398)  评论(0编辑  收藏  举报