|
楼主 |
发表于 2008-2-7 12:38:00
|
显示全部楼层
回复: 检索并删除数据库里的重复记录
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断2 R5 b- K: D6 _- w/ U
select * from people
) Z) {: Z( u' p( ]where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
* z& P Y3 g: E8 T Ydelete from people 3 v8 c, ^( n# o7 o& m7 z
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)4 u7 s1 v2 u# |4 g
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)3、查找表中多余的重复记录(多个字段)
`5 J) A; b% y! p' }select * from vitae a( n% \5 g! \- g2 B9 F) C7 g$ |
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录# d. p9 E0 n) v$ H: t
delete from vitae a. J/ ]& w" R( l( V( x& R5 W
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)
+ w: x, p6 }# L8 w9 N# B) ^5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录( q, r1 t0 f+ h9 O
select * from vitae a% Z/ N4 u+ b: b7 Y ~' F
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
* \5 s) z9 s/ a# \8 dand rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
( \- n4 a9 O x5 h(二)) `* h8 L0 h+ C2 V
比方说
+ J% g J) A# a% g; L8 f' c8 R0 g在A表中存在一个字段“name”,! f0 X2 A5 G: t
而且不同记录之间的“name”值有可能会相同,
6 n; [, j( i ]" }; z现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;
, h! S$ C4 K* l; JSelect Name,Count(*) From A Group By Name Having Count(*) > 1如果还查性别也相同大则如下:5 d& G- V9 k: ?' x. m
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1 |
|