美文网首页
sql 删除重复数据

sql 删除重复数据

作者: 巡山的小猴子 | 来源:发表于2018-07-26 12:58 被阅读0次

    第一步查出重复数据

        select id from phone_blacklist where (userid,phone) in 
        (
            select userid,phone from phone_blacklist 
            group by userid,phone having count(*) > 1 
        )
    

    第二步排除不想删除的第一条

        select id from phone_blacklist where (userid,phone) in 
        (
            select userid,phone from phone_blacklist 
            group by userid,phone having count(*) > 1 
        )
        and id not in
        (
            select min(id) from phone_blacklist 
            group by userid,phone having count(*) > 1 
        )
    

    第三步开始删

    delete from phone_blacklist where id in
    (
        select id from
        (
            select id from phone_blacklist where (userid,phone) in 
            (
                select userid,phone from phone_blacklist 
                group by userid,phone having count(*) > 1 
            )
            and id not in
            (
                select min(id) from phone_blacklist 
                group by userid,phone having count(*) > 1 
            )
        ) as a
    );
    

    相关文章

      网友评论

          本文标题:sql 删除重复数据

          本文链接:https://www.haomeiwen.com/subject/wsqbmftx.html