mysql——查询重复数据,及删除重复数据只保留一条数据

查询 text 表中,user_name字段值重复的数据及重复次数

select user_name,count(*) as count from text 
group by user_name having count>1

 

删除 text 表中,重复出现的数据只保留 ID 最大的一条数据,没有重复的数据不删除。

delete from text where DW_DATE=20171227 AND
 id not in( select id from (select max(id) as id,count(user_name) as count from text
WHERE DW_DATE=20171227
group by user_name having count =1 order by count desc) as tab)
AND
id not in( select id from (select max(id) as id,count(user_name) as count from text
WHERE DW_DATE=20171227
group by user_name having count >1 order by count desc) as tab)

 

posted @ 2018-01-03 10:53  断尘  阅读(4136)  评论(0编辑  收藏  举报