数据库sql去重

2019-03-07  本文已影响0人  忠犬_1c67

1 去重
1.1 查询
1.1.1 存在部分字段相同的纪录,即有唯一键主键ID
最常见情况如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组
select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])
1.1.2 存在两条完全相同的记录用关键字distinct就可以去掉
select distinct id(某一列) from table(表名) where (条件)
1.1.3 查找表中不含重复的数据,根据单个字段(id)来判断
select * from table where id in (select id from table group by id having count (id) >1)
1.1.4 查找表中重复的数据,根据单个字段(id)来判断
select * from table where id not in (select id from table group by id having count (id) >1)

1.1.5 查询全部的重复信息
select * from people where id not in (select min(id) from people group by name,sex HAVING COUNT(*) < 2)
1.1.6 查询全部的重复信息
select * from table where id not in (select MIN(id) from table group by name,sex)
1.1.7 删除多余重复的信息,只保留最小ID
delete from table where id not in(select MIN(id) from table group by name,sex)

上一篇下一篇

猜你喜欢

热点阅读