|
楼主 |
发表于 2008-2-7 12:38:00
|
显示全部楼层
回复: 检索并删除数据库里的重复记录
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断/ k8 T ~, Z- S
select * from people
) r/ [- J$ ]7 Y- C/ @# m7 }where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
& g$ T, e$ o6 u! I/ Ddelete from people
, }% P" X# B7 s# R dwhere peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
% Y ]/ E3 y) k5 ^and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)3、查找表中多余的重复记录(多个字段)
: {( |$ m+ p4 i" gselect * from vitae a
( j8 ]0 u q& Q; n- |. \& r0 O. u- Y" awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
' U: |7 G6 F( W" g: Q" E7 U5 ?delete from vitae a& F: R( O/ s& P) t* [
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)
. |- M5 D+ e$ [7 Q# `9 Y5 U5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
4 _+ x6 |# a0 @% m, Z' lselect * from vitae a
+ Q- w z& L5 G0 o2 L( T; |where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1), \" T. a( w8 ~! O) G
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
, u) @5 a2 ]5 J: _" Q% n3 I; z(二)
" y9 x; V# r7 ^比方说
- t9 ~6 C8 q1 K s; y在A表中存在一个字段“name”,4 ]9 y$ D" Y" n$ {0 r2 N
而且不同记录之间的“name”值有可能会相同,4 n) u3 A0 p+ K! u0 N
现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;0 V) w3 C; B) T5 @$ D. Y7 V4 w
Select Name,Count(*) From A Group By Name Having Count(*) > 1如果还查性别也相同大则如下:* [) s( C: }# M) j# U: t# |& }
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1 |
|