• MySQL 删除数据库中重复数据方法


    1. 查询需要删除的记录,会保留一条记录。

    select a.id,a.subject,a.RECEIVER from test1 a left join (select c.subject,c.RECEIVER ,max(c.id) as bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b on a.id< b.bid where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid
    

     2. 删除重复记录,只保留一条记录。注意,subject,RECEIVER 要索引,否则会很慢的。

    delete a from test1 a, (select c.subject,c.RECEIVER ,max(c.id) as bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid;
    

     3. 查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

    select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
    

     4. 删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

    delete from people where peopleId in (select peopleId from people group by peopleId  having count(peopleId) > 1) and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
    

     5.删除表中多余的重复记录(多个字段),只留有rowid最小的记录

    delete from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
    

     http://www.jb51.net/article/23964.htm

  • 相关阅读:
    栈的理解(出、入栈)
    javascript实现可以拖动的层示例(层拖动,兼容IE/FF)
    C# 队列 堆栈
    从0开始做Windows Phone 7开发
    C#写系统日志
    一位软件工程师的6年总结
    向Android模拟器发短信打电话
    office2010激活方法
    常用正则表达式
    JaveScript获得鼠标位置
  • 原文地址:https://www.cnblogs.com/zhuiluoyu/p/7607921.html
Copyright © 2020-2023  润新知