数据库SQL去重,保留一条数据

利用SQL,删除掉重复多余的数据,并且只保留一条数据。

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

select * from team where teamId in (select teamId from team group by teamId having count(teamId) > 1)

2、删除表中多余的重复记录,重复记录是根据单个字段(teamId)来判断,只留有rowid最小的记录
delete from team where

teamName in(select teamName from team group by teamName having count(teamName) > 1)

and teamId not in (select min(teamId) from team group by teamName having count(teamName)>1)

1、查找表中多余的重复记录(多个字段)
select * from team t
where (t.teamId,t.teamOrg) in (select teamId,teamOrg from team group by teamId,teamOrg having count(*) > 1)

2、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from team t
where (t.teamId,t.teamOrg) in (select teamId,teamOrg from team group by teamId,teamOrg having count(*) > 1)
and rowid not in (select min(rowid) from team group by teamId,teamOrg having count(*)>1)

3、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from team t
where (t.teamId,t.teamOrg) in (select teamId,teamOrg from team group by teamId ,t.teamOrg having count(*) > 1)

and rowid not in (select min(rowid) from team group by teamId,teamOrg having count(*)>1)   

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

update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%'

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

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

1.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录
update team set ispass=-1
where teamId in (select teamId from team group by teamId

1.oracle中利用rowId去删除多余的数据:


(1).oracle中,每一条记录都有一个rowidrowid在整个数据库中是唯一的,rowid确定了每条记录是在Oracle中的哪一个数据文件、块、行上。

 

(2).在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中那些具有最大rowid的就可以了,其余全部删除。

delete from team where teamName in

(select* from team t where rowid=(select max(rowid) from team where teamName=t.teamName))

你可能感兴趣的:(数据库SQL去重,保留一条数据)