当前位置: 移动技术网 > IT编程>数据库>其他数据库 > SQL语句实现删除重复记录并只保留一条

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

2017年12月08日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下: 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)

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网