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

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

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

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

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

×
conn.execute("delete from table where id not in (select distinct from table)")" k! M- d* O) E7 A& V& m 文件删除函数 <% ' u' x# f" M" i2 v' Q7 H- s0 \) t3 h'文件删除函数 6 h$ y5 A8 L8 s+ z. x8 y( Y function deletefile(filename) # F' X3 V9 [5 f6 [& x) F8 X$ n; _ V if filename<>"" then ' ^ V8 h+ ?; n" b set fso=server.CreateObject("scripting.filesystemobject") $ Y5 d7 m+ @+ H- G# q, J* Tif fso.FileExists(filename) then 4 {5 o+ y5 R) q2 G& J' S; a fso.DeleteFile filename * B/ r$ q# \; n" J1 E; ~+ G+ C0 selse , g9 d' j9 I( u! G, h Y5 Q" XResponse.Write "<script>alert(’该文件不存在’);</script>" : j! c& h+ G7 ?1 D1 f2 L1 g7 l end if % e7 L* D j! |5 |' J6 t$ p end if 1 L% p8 ] n- j I7 _* i" v: c; send function , _7 I' `0 Y; |) i- R4 J - N( P! \% j4 ]strfile=server.MapPath("fileName") ) ]) b1 b' O* Z- L8 h R% c) W deletefile(strfile) + l$ K: `& C9 `" a$ V$ D1 I8 N%>
遨海湾-心灵的港湾 www.aosea.com
 楼主| 发表于 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
遨海湾-心灵的港湾 www.aosea.com
回复

使用道具 举报

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

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

(三)2 o0 ?1 ~5 k. {' E X; L7 o$ ^3 a 方法一 ! k1 ]. T2 b* ? # M& \! B* F- s |3 E0 z( Bdeclare @max integer,@id integer7 d9 `$ F2 i- O0 e* d 3 _- |; w# b: F9 ?6 h& s% jdeclare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 10 P; Y) n7 C. B4 u6 c3 P8 a : d% _ R9 X4 I0 F$ u% o( C5 H3 ~# `open cur_rows' l; e0 L2 m' I; F5 K 2 W5 M; S5 i ?5 p" P% e& Jfetch cur_rows into @id,@max 7 P! ^) {4 d- Q- l- L3 @) N; H3 ]$ }+ c2 I4 x( E5 a. U while @@fetch_status=0 8 y; o) I: q" d4 K+ }1 L; O1 C' k5 s4 K7 [) [, M% H* |3 X begin) v$ R1 X4 `7 W# p ! |7 q" d2 X! V% v- d select @max = @max -1 u8 C+ V s7 m) }2 _; W8 q 0 I$ w& v# h) I, ^7 A' ]set rowcount @max " Q. d# ~# i5 t& a% T9 g4 Q& L3 x5 J4 N$ ?, X3 f delete from 表名 where 主字段 = @id $ Z* c3 W4 e! l* N9 U* l % S5 t" H; O5 _$ ^1 Wfetch cur_rows into @id,@max( P6 j1 a9 X' b) R8 ~; E( b+ }1 K end5 {5 c+ P/ U% F & E+ R3 }' Z3 F8 t iclose cur_rows 0 g" X/ O. T' Y4 n b' k ( f$ _/ a4 y' N6 P' e( Rset rowcount 0 $ F8 Q1 B7 X7 i- a& ]' @& K 6 W5 i3 D3 q4 ~3 U8 P方法二 ) o4 p' U3 L; g) `" x/ y 3 k. Z. ^7 {3 `"重复记录"有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。5 e$ N& t* Z+ l- T2 `; o. u9 _0 ~ + k1 ?+ r4 x v4 f; i5 ^   1、对于第一种重复,比较容易解决,使用; l D: c$ X$ x# q+ R# w F3 q9 d* U( Q# D6 d$ @1 C) ? select distinct * from tableName1 C9 Z4 @+ X0 j% c! q- N " e9 K( P& C2 O! D4 n9 T |7 f   就可以得到无重复记录的结果集。0 Z4 T; q, f7 d: _" Y % M+ ?2 R/ W. @1 q, Z! l2 D m2 t   如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除 % M" V' |3 y5 ~' V* @% g# e" ~7 g& m/ ?' P! U1 B select distinct * into #Tmp from tableName5 ~: y% Q- G9 {8 W2 Q 2 X, I% o+ k ~% ^ drop table tableName & J$ Z1 Y6 U! f5 @ ; R) V; j7 @; C0 |8 M- Iselect * into tableName from #Tmp 0 `* z5 ~* j( f: n& c$ g7 K( k. T select id from tablename ! j3 ?+ C/ C- J$ o$ t h7 g; j2 X 9 d6 O& C) n# ygroup by id . [ Q3 t1 a; d: s, z, L& |! g4 E 2 f; {! k) A; m3 T8 _ having count(id) > 17 X& z, q# E+ W6 }+ \- l. | * v+ [, ^# i( G, T )
遨海湾-心灵的港湾 www.aosea.com
回复

使用道具 举报

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

本版积分规则

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

QQ|手机版|小黑屋|遨海湾超级社区 网站统计

GMT+8, 2025-4-4 20:27

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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