欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL语句实现删除重复记录并只保留一条

程序员文章站 2022-06-24 22:49:46
复制代码 代码如下: delete weibotopics where id in(select max(id) from weibotopics group by we...

复制代码 代码如下:

delete weibotopics where id in(select max(id) from weibotopics group by weiboid,title having count(*) > 1);

sql:删除重复数据,只保留一条用sql语句,删除掉重复项只保留一条在几千条记录里,存在着些相同的记录,如何能用sql语句,删除掉重复的呢

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

复制代码 代码如下:

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

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleid)来判断,只留有rowid最小的记录
复制代码 代码如下:

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

3、查找表中多余的重复记录(多个字段)

复制代码 代码如下:

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)

5、查找表中多余的重复记录(多个字段),不包含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) 

6.消除一个字段的左边的第一位:

复制代码 代码如下:

update tablename set [title]=right([title],(len([title])-1)) where title like '村%'

7.消除一个字段的右边的第一位:

复制代码 代码如下:

update tablename set [title]=left([title],(len([title])-1)) where title like '%村'

8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录

复制代码 代码如下:

update vitae set ispass=-1 where peopleid in (select peopleid from vitae group by peopleid,seq having count(*) > 1) and seq in (select 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)