找回密码
 入住遨海湾
搜索
网站解决方案专享优惠-3折上云
查看: 1638|回复: 2

检索并删除数据库里的重复记录

[复制链接]
发表于 2008-2-7 12:15:00 | 显示全部楼层 |阅读模式

登录后查才能浏览下载更多咨询,有问题联系QQ:3283999

您需要 登录 才可以下载或查看,没有账号?入住遨海湾

×
conn.execute("delete from table where id not in (select distinct from table)"). y( U/ T5 S4 N7 _) i* v 文件删除函数 <% ! ]: [4 x% u7 O' }" l '文件删除函数 5 c: h+ J. J4 |1 T function deletefile(filename) 3 ^! V! W' s( S m$ d% _if filename<>"" then 3 }7 j" [# R" @# y8 v' [set fso=server.CreateObject("scripting.filesystemobject") ' i$ P' x/ P3 `$ ?: A if fso.FileExists(filename) then 9 Q, b/ h- M4 k$ ?+ I2 qfso.DeleteFile filename 1 b) K6 q, E( M. j2 ]" Relse $ w8 u# _ ]4 u5 t Response.Write "<script>alert(’该文件不存在’);</script>" . l, D* V* z+ t6 ^* T end if 3 {) i. `* u/ r6 O/ ^# Dend if 4 N+ j" |# h7 V$ [8 q5 B end function + h9 ]) y( H, P/ g6 W9 N, S 0 j: N# }5 f6 ]/ e0 ] [ strfile=server.MapPath("fileName") & Z5 E3 E0 T: k9 i* i deletefile(strfile) 0 d- ?3 Q7 q S' x' M& x5 V4 B %>
遨海湾-心灵的港湾 www.aosea.com
 楼主| 发表于 2008-2-7 12:38:00 | 显示全部楼层

回复: 检索并删除数据库里的重复记录

 1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 0 z7 _" b' N; V7 }4 |* A9 \6 [select * from people 8 F7 g- e+ ~2 J' d( ewhere peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录 & i: }9 c' G0 ~7 u1 N7 ~( edelete from people ; `. }+ {+ L8 P7 x4 m/ R G- z* \ where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1). ~( t2 w: G$ H( V2 |' B0 R# J and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)3、查找表中多余的重复记录(多个字段) ; }$ y) f/ H* ]) t1 g select * from vitae a7 R8 C) u% R$ x" V% [: H2 C where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录 . g. r6 q# r a3 Q" Y- Y$ Edelete from vitae a & t! }8 K4 ^* X* c- owhere (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)9 S; ]$ v0 L+ n; D2 k 5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录 : W& {8 b+ ~3 p1 B! b# ^. y5 o- Zselect * from vitae a$ u: H/ a. z: x9 ^& ? where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) & Z* @2 s0 R( e# oand rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)6 v7 w/ z) L' T' }( Q3 d" n' e: K5 v (二) / K3 o; r/ L+ I2 w+ y比方说7 G @) q9 x5 P8 {; X* u4 n( x) j 在A表中存在一个字段“name”, . t) i$ `2 s3 B z' S, M j, f而且不同记录之间的“name”值有可能会相同,% V% h3 i2 u; g# m* K9 ^ 现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项; 2 i; S4 g4 \( ~: TSelect Name,Count(*) From A Group By Name Having Count(*) > 1如果还查性别也相同大则如下:8 }* ?9 e* s$ t Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1
遨海湾-心灵的港湾 www.aosea.com
回复

使用道具 举报

 楼主| 发表于 2008-2-7 12:41:00 | 显示全部楼层

回复:检索并删除数据库里的重复记录

(三) ! {( ?% g; B5 s6 G, _$ N2 q0 |方法一 2 r$ ?0 H# }2 z3 t1 v 0 _2 o, ?3 M& m& ?$ n7 Hdeclare @max integer,@id integer 1 ?- G9 g# v `& z7 m: J& S- W% s' [! o- @( e4 Q! N& a) |3 u/ q o" n declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1: r4 w a% y# I& L9 C6 M) U: r" k % y0 U3 U9 q1 C0 Qopen cur_rows 3 E% }" t5 Z7 w2 y0 i+ ~0 p/ q# C1 Y- S# B fetch cur_rows into @id,@max' W; Q+ o- C1 F. ]; E) }9 i 7 K0 Z6 M. W/ c; W5 W4 Wwhile @@fetch_status=08 u8 q0 B' o8 M) I$ E, O9 e # x* s1 h W; \ begin" k+ @8 r0 @& w, v5 K# f ) h. r i2 a+ E9 p& c/ O! A9 n5 u select @max = @max -1 1 C2 l8 R9 p1 X8 ]$ r8 A' K" W, _& ]9 N set rowcount @max- h. s$ j% G3 r- J, p- K7 @# d 0 k t5 N/ ?. V: T5 l8 f1 X. Jdelete from 表名 where 主字段 = @id 8 G8 T8 N& N8 A8 C. ^* Z + [7 \# ~1 d' _& nfetch cur_rows into @id,@max 3 U* D9 K# W. e8 D0 h) S) Kend' g% Z* _3 P) F# v, g4 U6 F$ _- k % s: k" z4 L5 |0 [# q" Y/ A! ~ close cur_rows . \; M, O/ F) w7 m9 n; _* M6 m! s9 F' v( t& P set rowcount 0 : _( B, m' r! M, G2 ]0 J6 o& J4 A+ M' {2 t; | 方法二 # B- J/ Y/ H( A% P3 p M8 Z0 ?% P- q( q "重复记录"有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。 3 N* y# k/ k T/ j7 v2 L2 K/ Q, |! ]2 f# C* _) |   1、对于第一种重复,比较容易解决,使用+ m+ s' l& `! n- \2 n8 i. f# Q K * G' [6 V4 K! f5 w5 qselect distinct * from tableName : ^3 R# y1 X5 `9 x) S% F8 I N1 L2 M0 h   就可以得到无重复记录的结果集。1 E# l# ]4 a) D2 } 5 \: \* b4 R- z) }8 U, B   如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除 4 S/ m+ j# j& `' i& h) y* u+ ]5 ~4 o Z6 u& o select distinct * into #Tmp from tableName4 W7 ]- D [* S8 \0 O $ K' b5 {0 C+ r. o% k+ Idrop table tableName + t4 ?6 s1 L4 j; `1 [" w. p8 a 8 M$ Z0 [1 |1 V# p$ M7 v- V( U# |select * into tableName from #Tmp* K) Q7 U+ u; o" ~' Y $ |' y# m3 t- [' l* A! U- Fselect id from tablename ) y" |/ r. u1 t1 e 2 h/ M) p4 O; y, k b group by id 6 d2 A$ z$ G+ S4 ~, W; L7 ~ 0 U( T$ z2 `4 x) r- Q& I+ Phaving count(id) > 1 ) h) t+ Y; c3 h: c) @) O& L / r% E6 q0 B% R, N* P b: B, @)
遨海湾-心灵的港湾 www.aosea.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 入住遨海湾

本版积分规则

网站解决方案专享优惠-3折上云

QQ|手机版|小黑屋|遨海湾超级社区

GMT+8, 2024-11-21 20:41

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表