2015第17周六去除表中某字段重复记录

之前有个疑问:如何删除数据库表中某列有重复的数据记录(保留重复记录中的一条)?如下表a

x | y
---+---
1 | a 
2| b
3| a
4 | c

如果用distinct,select distinct(y)  from a的话正常,但要输出x、y的话不行,思考很久没想到用除分组函数外的其它解决方法;

select y,max(x) from a group by y having count(x) >1查询可以找出所有字段y有重复的记录列,然后在删除语句中限定y列在这个子查询,x列不在这个子查询中即可

delete from a where y in (select y from a group by y having count(x) >1)and x not in(select max(x) from a group by y having count(x)>1

原文地址:https://www.cnblogs.com/doit8791/p/4457075.html