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

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

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

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

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

×
conn.execute("delete from table where id not in (select distinct from table)") & R/ ]6 |. t. d/ ]- m) S8 c文件删除函数 <% ( R, }) K$ k. i# ?& s! u Z' p'文件删除函数 9 b( N; e7 D- h function deletefile(filename) 1 t& ]7 O5 f2 v4 E: M1 yif filename<>"" then ; T2 r4 `) k$ Bset fso=server.CreateObject("scripting.filesystemobject") 9 w# y$ R4 f4 U: h6 Q' S* { if fso.FileExists(filename) then 9 h( R: }: F) u% @$ X9 _. e+ E fso.DeleteFile filename 1 a. C; z; |7 D0 v) y# F( | else , Z' ~/ j1 r" X Response.Write "<script>alert(’该文件不存在’);</script>" 6 O) N+ t0 K. pend if 6 r5 M: |- Q5 H- jend if ! C' |# ?& _* F( r8 b2 ^! fend function ; J4 U0 [- b9 L . N3 M. F! a/ `0 g4 f0 z+ Sstrfile=server.MapPath("fileName") ( n/ W2 L! t; g4 E* n [deletefile(strfile) 4 K8 x. q8 D! w2 Y) J# p3 ]9 }%>
遨海湾-心灵的港湾 www.aosea.com
 楼主| 发表于 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
遨海湾-心灵的港湾 www.aosea.com
回复

使用道具 举报

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

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

(三). [: b2 Z, e& y; E( F8 s 方法一; c1 M7 `2 N( d1 X6 q+ _7 { R 2 z# ~7 t7 n( U( {. B7 Q: p7 Q5 R declare @max integer,@id integer 7 `: ~9 m2 u7 h3 [6 `8 k& V9 k- E6 C$ k8 J& J- I: Z2 A declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1 % Q% c) c* f: B3 b2 P. ?9 a " _" @7 M+ t V7 A2 Y5 wopen cur_rows $ ~5 a4 k& o- N8 b: p, K% T; | v# c6 _+ S! Yfetch cur_rows into @id,@max Y0 ^' i2 s5 \1 Q) l# t ; ]% ~. F4 p( J4 K. I P while @@fetch_status=0 8 I1 L: D2 `2 s0 }. ~) B n + }$ l/ O/ Q+ [. ~2 p, qbegin p* `4 L, R" R 0 ~3 j+ D4 x: h% ] c5 R% d9 pselect @max = @max -1 3 _% c' _( o2 g& l # f( B8 ~) {9 M) A" oset rowcount @max , L" x) U) J v5 h3 k i % N& s9 I% ~5 W" ?) o$ x6 Vdelete from 表名 where 主字段 = @id% T$ F' z7 Z4 p7 j 8 P1 ?8 e& S- v0 L fetch cur_rows into @id,@max5 I. U( |' X5 |0 `5 ~ end/ }; a- p( K4 j 8 V7 J1 z* g9 p' d& W& a# s# c close cur_rows6 D% C$ M0 N9 ~3 ~ $ z5 `7 K. H% C" `8 w set rowcount 0 5 \5 K7 x: g2 K! }. B f/ W5 ~, x( {3 I0 ` 方法二 ' @& l: W$ F" c* V- O5 _! t# p& T4 H" J "重复记录"有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。 1 y$ P; c" |4 ]3 r* r' k% U4 N' u; \5 U" T1 F5 T/ U   1、对于第一种重复,比较容易解决,使用# ?/ \6 A7 G! f% N. { / f- n& x& X' x) O( ~select distinct * from tableName# \/ n' e5 {% P* ~) b8 e' W4 q $ B5 f' @; m! {. \) F   就可以得到无重复记录的结果集。 * i/ R: f# N ?2 h; Y" C- l7 u- w D   如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除 . C/ k( G- ]8 p9 h; Z8 {9 M( V- _7 ~( d: L select distinct * into #Tmp from tableName 1 g w& i& |, s" ?& O* Q . \ j7 R) d4 h0 {7 y4 Fdrop table tableName1 @/ S, v% K" R0 [; |% w' t . o" B( x8 Q% Xselect * into tableName from #Tmp 1 ]9 O9 e- m6 ]' m ( }- p6 ?' T6 x6 ^ }select id from tablename # W' ^0 X* Q7 }# t V. C 3 N) O' i+ ~2 r/ A! Zgroup by id : [% j" H$ V8 t+ D+ f# [3 Y 9 h. u2 u- N' a$ e0 x- ?/ E having count(id) > 1/ h# W' L2 J4 s& Q, _ . i$ W( M* M4 @7 @! @ )
遨海湾-心灵的港湾 www.aosea.com
回复

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2024-11-22 08:09

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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