Sql去重及查询重复

1、最简单的是使用distinct,去除所有查询字段信息一样的数据

    

—————————————以下为有主键的情况下使用

2、 查询,去重,保留最小id信息

select * from people where id in (
    select MIN(id) from people group by name,sex)

3、 查询全部的重复信息

select * from people where id not in (

    select min(id) from people group by name,sex HAVING COUNT(*) < 2)


4、查询多余的重复信息

select * from people where id not in (

    select MIN(id) from people group by name,sex)


5、删除多余重复的信息,只保留最小ID

delete from people where id not in(
    select MIN(id) from people group by name,sex
)


    原文作者:pjwtheking
    原文地址: https://blog.csdn.net/u012860938/article/details/50791960
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞