【MySQL刷题笔记】和“重复”有关的问题

筛选重复字段

筛选出有重复的字段

select species from pet group by species;

筛选重复次数超过x的字段

select species from pet group by species having count(*)>x;

剔除重复数据,保证表中数据的唯一性:

例如上面的表中有很多重复的人名字,可以通过下面的语句筛除重复值:

delete from clients where id not in(select t.new_id from (select max(id) as new_id from clients group by names) as t);

由于在mysql中,不能先select一个表的记录,再按此条件进行更新和删除同一个表的记录,
必须重新select并对衍生出来的表起个别名。
删除后的结果为:

发布了390 篇原创文章 · 获赞 27 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/AXIMI/article/details/104296420