您的位置:68399皇家赌场 > 虚拟主机 > mysql删除重复记录语句的不2诀窍

mysql删除重复记录语句的不2诀窍

发布时间:2019-05-05 23:20编辑:虚拟主机浏览(53)


    mysql删除重复记录语句的点子,mysql删除记录语句

    例如:
    id name value
    1 a pp
    2 a pp
    3 b iii
    4 b pp
    5 b pp
    6 c pp
    7 c pp
    8 c iii
    id是主键
    务求取得这样的结果
    id name value
    1 a pp
    3 b iii
    4 b pp
    6 c pp
    8 c iii
    方法1
    delete YourTable
    where [id] not in (
    select max([id]) from YourTable
    group by (name value))
    方法2
    delete a
    from 表 a left join(
    select (id) from 表 group by name,value
    )b on a.id=b.id
    where b.id is null
    询问及删除重复记录的SQL语句
    询问及删除重复记录的SQL语句
    一、查找表中剩下的重复记录,重复记录是依赖单个字段(peopleId)来判别
    select * from people
    where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
    二、删除表中多余的重复记录,重复记录是依附单个字段(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)
    三、查找表中剩下的重复记录(八个字段)
    select * from vitae a
    where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
    4、删除表中多余的重复记录(三个字段),只留有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)
    伍、查找表中多余的重复记录(八个字段),不包罗rowid最小的笔录
    select * 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)
    (二)
    比方说
    在A表中留存贰个字段“name”,
    再就是不同记录之间的“name”值有相当大恐怕会一样,
    明天便是内需查询出在该表中的各记录之间,“name”值存在双重的项;
    Select Name,Count(*) From A Group By Name Having Count(*) > 1
    一经还查性别也同等大则如下:
    Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1
    (三)
    方法一
    declare @max integer,@id integer
    declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1
    open cur_rows
    fetch cur_rows into @id,@max
    while @@fetch_status=0
    begin
    select @max = @max -1
    set rowcount @max
    delete from 表名 where 主字段 = @id
    fetch cur_rows into @id,@max
    end
    close cur_rows
    set rowcount 0 方法二
    "重复记录"有八个意思上的重复记录,1是完全重复的记录,也即具备字段均再一次的记录,2是有些人命关天字段重复的记录,举例Name字段重复,而别的字段不必然再一次或都再度可以忽略。
      1、对于第二种重复,相比较便于化解,使用
    select distinct * from tableName
      就足以获取无重复记录的结果集。
      假如该表须求删除重复的笔录(重复记录保留1条),能够按以下情势删除
    select distinct * into #Tmp from tableName
    drop table tableName
    select * into tableName from #Tmp
    drop table #Tmp
      爆发那种重新的缘故是表设计不周发生的,增添唯一索引列就能够缓和。
      二、那类重复难题普通供给保存重复记录中的第二条记下,操作方法如下
      假设有重复的字段为Name,Address,供给赢得那多个字段唯1的结果集
    select identity(int,1,1) as autoID, * into #Tmp from tableName
    select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID
    select * from #Tmp where autoID in(select autoID from #tmp2)
      倒数select即获取了Name,Address不另行的结果集(但多了1个autoID字段,实际写时得以写在select子句中节省此列)
    (四)
    查询重复
    select * from tablename where id in (
    select id from tablename
    group by id
    having count(id) > 1
    )
    上学sql有一段时间了,开采在自己建了三个用来测试的表(未有建索引)中冒出了多数的重复记录。后来总结了部分删消肉复记录的方法,在Oracle中,能够经过唯1rowid完结删除重复记录;还是能建一时半刻表来落实...这些只提到其中的三种轻便实用的主意,希望得以和豪门享用(以表employee为例)。
    SQL> desc employee
    Name Null? Type


    emp_id NUMBER(10)
    emp_name VARCHAR2(20)
    salary NUMBER(10,2)
    能够经过下边包车型大巴话语查询重复的记录:
    SQL> select * from employee;
    EMP_ID EMP_NAME SALARY


    1 sunshine 10000
    1 sunshine 10000
    2 semon 20000
    2 semon 20000
    3 xyz 30000
    2 semon 20000
    SQL> select distinct * from employee;
    EMP_ID EMP_NAME SALARY


    1 sunshine 10000
    2 semon 20000
    3 xyz 30000
    SQL> select * from employee group by emp_id,emp_name,salary having count (*)>1
    EMP_ID EMP_NAME SALARY


    1 sunshine 10000
    2 semon 20000
    SQL> select * from employee e1
    where rowid in (select max(rowid) from employe e2
    where e1.emp_id=e2.emp_id and
    e1.emp_name=e2.emp_name and e1.salary=e2.salary);
    EMP_ID EMP_NAME SALARY


    1 sunshine 10000
    3 xyz 30000
    2 semon 20000

    1. 除去的二种艺术:
      (1)通过创建一时半刻表来落实
      SQL>create table temp_emp as (select distinct * from employee)
      SQL> truncate table employee; (清空employee表的数额)
      SQL> insert into employee select * from temp_emp; (再将一时表里的内容插回来)
      ( 二)通过唯1rowid完毕删除重复记录.在Oracle中,每一条记下都有多少个rowid,rowid在全体数据库中是唯一的,rowid明确了每条记下是在Oracle中的哪三个数据文件、块、行上。在再一次的笔录中,大概装有列的剧情都毫发不爽,但rowid不会一仍其旧,所以假若分明出重复记录中那多少个具备最大或纤维rowid的就能够了,别的全数删减。
      SQL>delete from employee e2 where rowid not in (
      select max(e1.rowid) from employee e1 where
      e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);--这里用min(rowid)也可以。
      SQL>delete from employee e2 where rowid <(
      select max(e1.rowid) from employee e1 where
      e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and
      e1.salary=e2.salary);
      (三)也是因而rowid,但效能越来越高。
      SQL>delete from employee where rowid not in (
      select max(t1.rowid) from employee t1 group by
      t1.emp_id,t1.emp_name,t1.salary);--这里用min(rowid)也可以。
      EMP_ID EMP_NAME SALARY

    1 sunshine 10000
    3 xyz 30000
    2 semon 20000
    SQL> desc employee
    Name Null? Type


    emp_id NUMBER(10)
    emp_name VARCHAR2(20)
    salary NUMBER(10,2)
    能够由此上边包车型大巴讲话查询重复的笔录:
    SQL> select * from employee;
    EMP_ID EMP_NAME SALARY


    1 sunshine 10000
    1 sunshine 10000
    2 semon 20000
    2 semon 20000
    3 xyz 30000
    2 semon 20000
    SQL> select distinct * from employee;
    EMP_ID EMP_NAME SALARY


    1 sunshine 10000
    2 semon 20000
    3 xyz 30000
    SQL> select * from employee group by emp_id,emp_name,salary having count (*)>1
    EMP_ID EMP_NAME SALARY


    1 sunshine 10000
    2 semon 20000
    SQL> select * from employee e1
    where rowid in (select max(rowid) from employe e2
    where e1.emp_id=e2.emp_id and
    e1.emp_name=e2.emp_name and e1.salary=e2.salary);
    EMP_ID EMP_NAME SALARY


    1 sunshine 10000
    3 xyz 30000
    2 semon 20000

    1. 除去的三种办法:
      (1)通过建构近期表来落到实处
      SQL>create table temp_emp as (select distinct * from employee)
      SQL> truncate table employee; (清空employee表的数码)
      SQL> insert into employee select * from temp_emp; (再将暂且表里的剧情插回来)
      ( 2)通过唯一rowid实现删除重复记录.在Oracle中,每一条记下都有三个rowid,rowid在方方面面数据库中是独一无二的,rowid鲜明了每条记下是在Oracle中的哪3个数据文件、块、行上。在再次的记录中,恐怕具有列的内容都无差异,但rowid不会同样,所以若是显著出重复记录中那么些具有最大或纤维rowid的就足以了,其他全部剔除。
      SQL>delete from employee e2 where rowid not in (
      select max(e1.rowid) from employee e1 where
      e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);--这里用min(rowid)也可以。
      SQL>delete from employee e2 where rowid <(
      select max(e1.rowid) from employee e1 where
      e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and
      e1.salary=e2.salary);
      (三)也是由此rowid,但作用更加高。
      SQL>delete from employee where rowid not in (
      select max(t1.rowid) from employee t1 group by
      t1.emp_id,t1.emp_name,t1.salary);--这里用min(rowid)也可以。
      EMP_ID EMP_NAME SALARY

    1 sunshine 10000
    3 xyz 30000
    2 semon 20000

    举例: id name value 一 a pp 2 a pp 三 b iii 四 b pp 5 b pp ⑥ c pp 7 c pp 捌c iii id是主键 供给猎取这么的结...



    1 sunshine 10000
    2 semon 20000
    SQL> select * from employee e1
    where rowid in (select max(rowid) from employe e2
    where e1.emp_id=e2.emp_id and
    e1.emp_name=e2.emp_name and e1.salary=e2.salary);
    EMP_ID EMP_NAME SALARY

    1 sunshine 10000
    1 sunshine 10000
    2 semon 20000
    2 semon 20000
    澳门皇家赌场55533网址,3 xyz 30000
    2 semon 20000
    SQL> select distinct * from employee;
    EMP_ID EMP_NAME SALARY

    1 sunshine 10000
    3 xyz 30000
    2 semon 20000
    SQL> desc employee
    Name Null? Type

    例如:
    id name value
    1 a pp
    2 a pp
    3 b iii
    4 b pp
    5 b pp
    6 c pp
    7 c pp
    8 c iii
    id是主键
    务求获得如此的结果
    id name value
    1 a pp
    3 b iii
    4 b pp
    6 c pp
    8 c iii
    方法1
    delete YourTable
    where [id] not in (
    select max([id]) from YourTable
    group by (name value))
    方法2
    delete a
    from 表 a left join(
    select (id) from 表 group by name,value
    )b on a.id=b.id
    where b.id is null
    询问及删除重复记录的SQL语句
    查询及删除重复记录的SQL语句
    一、查找表中多余的重复记录,重复记录是依照单个字段(peopleId)来推断
    select * from people
    where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
    二、删除表中多余的重复记录,重复记录是依赖单个字段(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)
    3、查找表中多余的重复记录(多个字段)
    select * from vitae a
    where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
    四、删除表中多余的重复记录(多个字段),只留有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)
    伍、查找表中剩下的重复记录(五个字段),不包罗rowid最小的记录
    select * 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)
    (二)
    比方说
    在A表中留存三个字段“name”,
    而且区别记录之间的“name”值有相当的大希望会同样,
    当今正是须求查询出在该表中的各记录之间,“name”值存在双重的项;
    Select Name,Count(*) From A Group By Name Having Count(*) > 1
    假如还查性别也一样大则如下:
    Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1
    (三)
    方法一
    declare @max integer,@id integer
    declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1
    open cur_rows
    fetch cur_rows into @id,@max
    while @@fetch_status=0
    begin
    select @max = @max -1
    set rowcount @max
    delete from 表名 where 主字段 = @id
    fetch cur_rows into @id,@max
    end
    close cur_rows
    set rowcount 0 方法二
    "重复记录"有八个意思上的重复记录,一是完全重复的记录,也即具有字段均再度的笔录,2是部分要害字段重复的笔录,比方Name字段重复,而别的字段不必然再次或都再一次能够忽略。
      一、对于第一种重复,相比轻便消除,使用
    select distinct * from tableName
      就能够获得无重复记录的结果集。
      假如该表必要删除重复的记录(重复记录保留1条),能够按以下措施删除
    select distinct * into #Tmp from tableName
    drop table tableName
    select * into tableName from #Tmp
    drop table #Tmp
      产生那种重新的原因是表设计不周发生的,扩充唯一索引列就可以减轻。
      2、那类重复难题一般供给保留重复记录中的第3条记下,操作方法如下
      假若有重复的字段为Name,Address,须求赢得那三个字段唯一的结果集
    select identity(int,1,1) as autoID, * into #Tmp from tableName
    select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID
    select * from #Tmp where autoID in(select autoID from #tmp2)
      最终一个select即得到了Name,Address不另行的结果集(但多了二个autoID字段,实际写时得以写在select子句中节约此列)
    (四)
    询问重复
    select * from tablename where id in (
    select id from tablename
    group by id
    having count(id) > 1
    )
    读书sql有一段时间了,发掘在本人建了2个用来测试的表(未有建索引)中出现了大多的重复记录。后来总括了一些刨除重复记录的主意,在Oracle中,能够因而唯1rowid完结删除重复记录;还足以建一时半刻表来贯彻...这些只提到里头的二种简易实用的艺术,希望能够和大家享受(以表employee为例)。
    SQL> desc employee
    Name Null? Type

    1 sunshine 10000
    3 xyz 30000
    2 semon 20000



    1 sunshine 10000
    2 semon 20000
    3 xyz 30000
    SQL> select * from employee group by emp_id,emp_name,salary having count (*)>1
    EMP_ID EMP_NAME SALARY

    emp_id NUMBER(10)
    emp_name VARCHAR2(20)
    salary NUMBER(10,2)
    能够通过上面包车型大巴讲话查询重复的笔录:
    SQL> select * from employee;
    EMP_ID EMP_NAME SALARY

    1 sunshine 10000
    2 semon 20000
    SQL> select * from employee e1
    where rowid in (select max(rowid) from employe e2
    where e1.emp_id=e2.emp_id and
    e1.emp_name=e2.emp_name and e1.salary=e2.salary);
    EMP_ID EMP_NAME SALARY


    本文由68399皇家赌场发布于虚拟主机,转载请注明出处:mysql删除重复记录语句的不2诀窍

    关键词: 68399皇家赌场