zoukankan      html  css  js  c++  java
  • mysql 删除表中的某列的重复字段

    假设:

    create table t(
    id int not null primary key auto_increment,
    name char(10) not null,
    sex char(2) not null
    )engine=myisam;
    
    
    insert into t values
    (null,'tom',''),
    (null,'jack',''),
    (null,'小文',''),
    (null,'小文',''),
    (null,'tom',''),
    (null,'小张',''),
    (null,'小赵',''),
    (null,'tom',''),
    (null,'jack',''),
    (null,'小赵','');

     

    删除重复字段SQL代码

    delete t as a
    from t as a,
    (select * from t group by name having count(1)>1) as b
    where a.name=b.name
    and a.id > b.id;

    详细解释

    1、首先把所有重复的第一个字段取出

    select * from t group by name having count(1)>1

    2、再与原表组成对照表

    select * from t as a,
    (select * from t group by name having count(1)>1) as b
    where a.name=b.name;

    3、取出重复第一个字段的id号以外的所有字段

    select * from t as a,
    (select * from t group by name having count(1)>1) as b
    where a.name=b.name
    and a.id > b.id;

    4、进行删除

    delete t as a
    from t as a,
    (select * from t group by name having count(1)>1) as b
    where a.name=b.name
    and a.id > b.id;

  • 相关阅读:
    Linux学习总结(18)——Linux使用init命令关机、重启、切换模式
    iOS autorelease使用详解
    iOS xib的使用详解
    iOS ASI--缓存
    iOS ASI--其他用法
    iOS ASI--管理多个请求
    iOS ASI--文件上传
    iOS ASI--文件下载
    iOS ASI--POST请求
    iOS ASI--GET请求
  • 原文地址:https://www.cnblogs.com/inuex/p/4326334.html
Copyright © 2011-2022 走看看