|
楼主 |
发表于 2008-2-7 12:38:00
|
显示全部楼层
回复: 检索并删除数据库里的重复记录
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
' _3 S/ H" M1 S. l; mselect * from people6 K, I5 D! p5 B% V* m! m9 [4 G
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
& z& g( n6 o2 ?" v- z0 Z* ^- ndelete from people 7 A" y/ `, c/ i
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) Q3 f+ R1 v( O+ M+ j! \
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)3、查找表中多余的重复记录(多个字段) / j% C# L( `. ?. G) Q ]
select * from vitae a8 j$ |) @" c# T. @) l' `7 Z5 w
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
) `2 @1 k4 o& Q& idelete from vitae a$ f& h7 z; L8 I2 K4 _
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)8 w6 O K( h6 C5 S
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
9 { m1 |' R, X( b9 l& m3 ^4 [, ?& nselect * from vitae a5 u" N+ z, g8 r# Z) t
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)8 t" d# I4 K" [$ }* V; C l* }
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)5 D' D' v! N+ }1 s* U
(二)
7 ^+ M! G4 @8 r$ y1 f0 e8 A比方说! }$ o. w0 l, m5 d$ f) g \7 Z
在A表中存在一个字段“name”,
: M7 x+ ~! _4 O7 Q3 u e, }6 o而且不同记录之间的“name”值有可能会相同,
$ f4 T- y, ?6 { K# w; e现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;
' @& m& l9 e0 S, g+ F2 qSelect Name,Count(*) From A Group By Name Having Count(*) > 1如果还查性别也相同大则如下:, V4 t) Z/ W4 w: J" r$ @* {
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1 |
|