在几千条记录里,存在着些相同的记录,如何能用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 peopleNamefrom 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
在sql的使用中,我们总是碰到需要删除重复数据的情况,但是又不能全部删除完,必须要保留至少一个重复的数据。重复的记录根据两个字段 a2,a3 判断(实际使用中可以拓展为多个)
在上述的表中第三行和第四行重复,我们要选择一行删除,流程如下:
结果如下:
得到的结果如下:
|a1|a2|a3|
|---|---|
|3|2|2|
|4|2|2|
|6|2|2|
那么后面就很好办了:
3.选出要删除的值:
结果是保留a1最小的值,其他选项全部选出,
请注意 此时并不是将Select 改为delete就可以了,如果你直接这样子改的话,会报如下错误:
该错误提示你,不能先select出同一表中的某些值,再update这个表(在同一语句中)。所以要稍微修改一下。
结果如下:
|a1|a2|a3|
|---|---|
|1|1|1|
|2|1|2|
|3|2|2|
|5|3|3|
完毕
注:如果说不用保留一行数据的话那么就简单多了,只需要一个很简单的sql语句:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)