zoukankan      html  css  js  c++  java
  • MySQL——删除重复数据

    前言

    数据导入的时候,导入了重复的数据

    内容

    结果

    delete from <table.name> where id in (select id from (select * from <table.name> where wxid in(select wxid from <table.name> group by wxid having count(wxid) >1) and id not in (select min(id) from <table.name> group by wxid having count(wxid)>1)) a);
    

    步骤

    查询重复数据的字段

    我这里是wxid

    select wxid from <table.name> group by wxid having count(wxid) >1;
    

    查询出重复数据字段中最小的自增ID

    select min(id) from <table.name> group by wxid having count(wxid)>1;
    

    筛选出将被删除的重复数据

    select * from <table.name> where wxid in(select wxid from <table.name> group by wxid having count(wxid) >1) and id not in (select min(id) from <table.name> group by wxid having count(wxid)>1);
    

    将需要被删除的自增ID筛选出来

    select id from (select * from <table.name> where wxid in(select wxid from <table.name> group by wxid having count(wxid) >1) and id not in (select min(id) from <table.name> group by wxid having count(wxid)>1)) a;
    

    根据ID删除重复数据

    ## 先通过select确认没有问题后再使用delete
    select * from <table.name> where id in (select id from (select * from <table.name> where wxid in(select wxid from <table.name> group by wxid having count(wxid) >1) and id not in (select min(id) from <table.name> group by wxid having count(wxid)>1)) a);
    
    ## 真正删除
    delete from <table.name> where id in (select id from (select * from <table.name> where wxid in(select wxid from <table.name> group by wxid having count(wxid) >1) and id not in (select min(id) from <table.name> group by wxid having count(wxid)>1)) a);
    
  • 相关阅读:
    获取各种屏幕宽度、高度
    java中获取本地文件的编码
    线程通信与进程通信的区别
    女性长期没有性生活有什么危害?
    面试中你必须要知道的语言陷阱
    当项目出现空闲时候,如何开展软件测试工作?
    同样是做鸭的 绝味与周黑鸭的区别咋那么大?!
    javaIO(二)
    (原)代码块
    this的使用
  • 原文地址:https://www.cnblogs.com/wangyang0210/p/13914474.html
Copyright © 2011-2022 走看看