在一家韩国电商的面试中被问到了,当时一脸懵逼,没想法……
回来想了想,还是有些思路的……唉……临场真是弱啊……
第一步:查询重复记录
SELECT * FROM TableName WHERE RepeatFiled IN ( SELECT RepeatFiled FROM TableName GROUP BY RepeatFiled HAVING COUNT(RepeatFiled) > 1 )
这一段逻辑很简单,就是把重复条数大于1的全部都搞出来就行了。
第二步:删除重复记录,只保留一条
SELECT * FROM TableName WHERE RepeatFiled IN ( SELECT RepeatFiled FROM TableName GROUP BY RepeatFiled HAVING COUNT(RepeatFiled) > 1 AND ID NOT IN ( SELECT MIN(ID) FROM TableName GROUP RepeatFiled HAVING COUNT(RepeatFiled) > 1 ) )在上一步的基础上继续过滤,那就把最小值留下就行了,大功告成!