当前位置:Gxlcms > 数据库问题 > mysql对表中数据根据某一字段去重

mysql对表中数据根据某一字段去重

时间:2021-07-01 10:21:17 帮助过:29人阅读

* from 表名 group by 根据哪一个字段(简称字段) having count(字段) > 1

注意:这是查出所有重复记录的第一条记录,需要保留,因此需要添加查询条件,查出所有的重复记录

select id,expect from cqssc where expect in (select expect from cqssc group by expect having count(expect)>1) 

and id not in(select min(id) from cqssc group by expect having count(expect)>1)

技术图片

 

然后

delete from cqssc where id in (select id from (select id from cqssc where expect in 

(select expect from cqssc group by expect having count(expect)>1) and id not in

(select min(id) from cqssc group by expect having count(expect)>1)) as tmpresult)

技术图片

删除成功,最后再查询一下看是否还有重复记录

技术图片

 

mysql对表中数据根据某一字段去重

标签:mamicode   let   expect   sql   HERE   font   重复记录   一个   删除   

人气教程排行