msql里有张表存有a,b两列,a存手机号,b存姓名,现在想剔除姓名重复的列,只保留姓名重复的其中一条记录


a b
1 张三
2 张三
两者只保留一个

delete from è¡¨å where a in (select min(a) from è¡¨å group by b)追问

a 列为手机号 min 函数适用于它吗?

追答

恩,适用,min或max都可以
只要每个人的手机号不重复,上边这句不会有问题

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-22
(1)delete from test where b in (select t1.b from (select t2.* from test as t2) as t1 group by b HAVING count(*) >1)
and a not in (select min(t3.a) from (select t4.* from test as t4) as t3 group by t3.b having count(*)>1 );
纯sql有点麻烦,上面那条可以实现,但数据一多性能不怎样

思路:获取到多条记录的b值;获取到重复记录中的a的最小值(这样才好保留一条记录);语句里面用了很多中间表,不能直接用test表,不然报错(删除过程中同时查找同个表)(2)新建一张相同的表,把b字段设置成唯一,然后再导数据进去,insert时使用ignore过滤掉重复的值。这种感觉挺好的,交给mysql处理
相似回答