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

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

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

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

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

×
conn.execute("delete from table where id not in (select distinct from table)")! z7 ~" S! J8 h 文件删除函数 <% 0 Y p+ j8 T, `'文件删除函数 & r/ |- Q9 v1 g# P function deletefile(filename) + \/ L; F) Z/ Z( @if filename<>"" then ' U5 ~8 U6 B3 Q set fso=server.CreateObject("scripting.filesystemobject") ! b6 G9 ~; y& ]5 i, c+ hif fso.FileExists(filename) then # `! {1 q' L8 j5 e" A2 p: d+ e fso.DeleteFile filename ' C$ d2 N5 O! o/ X" j0 \/ m6 \ else 2 E) \* s( a" @* M* f" l( l: r; {Response.Write "<script>alert(’该文件不存在’);</script>" ) X- h$ l' w5 J; A8 |5 T: i7 V$ g end if 1 w, J- q% e$ Y' J. l4 i6 w end if % c3 e/ p9 |/ P" F2 [7 J& } end function 7 r0 p2 g: @/ N! w. l! t5 w0 L3 ~, X 3 G0 v! d6 t- \6 Fstrfile=server.MapPath("fileName") ; X( Y- j2 u) q# t0 ideletefile(strfile) 1 A1 f7 |; k( E. V9 N! i%>
遨海湾-心灵的港湾 www.aosea.com
 楼主| 发表于 2008-2-7 12:38:00 | 显示全部楼层

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

 1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断0 Q) O- V: j: r0 o$ ]2 ? select * from people ) W, \2 _ Z6 @# pwhere peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录 - X2 ?2 L! c+ J' b6 q+ c: Jdelete from people . ~1 \6 d" N; r$ vwhere peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) " B/ n% a% G/ {; X3 x0 aand rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)3、查找表中多余的重复记录(多个字段) 0 T3 i$ t0 @" A2 W) |! O) Yselect * from vitae a6 |$ C. l$ c& _7 c where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录$ l \; N6 @% F, V delete from vitae a $ U: r/ `/ C5 s0 `, r- Vwhere (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) : \) {. Q& o5 o% }5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录 % {4 z* e4 @3 M+ uselect * from vitae a 8 J7 G7 H/ k& ^7 ?) @where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) , S& Q, j1 P7 X! x* Zand rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1) ' e2 p8 ]/ n, o9 D8 }(二)) W7 M Y: j: L% ^( z+ M 比方说 1 T7 y! h. ^* b6 ]5 N在A表中存在一个字段“name”,! Q' c, x3 B Q; W4 `1 J- E1 o+ A 而且不同记录之间的“name”值有可能会相同, 9 N4 |" S& A* X% d) n现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;# L' G- A/ g) q6 } Select Name,Count(*) From A Group By Name Having Count(*) > 1如果还查性别也相同大则如下: " p7 `+ p& \/ s) ~Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1
遨海湾-心灵的港湾 www.aosea.com
回复

使用道具 举报

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

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

(三)0 g( [( v' ?+ m$ S0 ]% L- u, ?) j 方法一 # t% n$ Y7 o& [. v: n% _ , n, u4 \+ f/ z, S* Adeclare @max integer,@id integer " _* Z8 w1 q* e0 A& y4 X# _ j' w: [( N4 Y: c# N7 M- J9 g4 C/ @ declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1 0 v5 B: X4 A& ~1 d$ ?" X+ y4 u' a Y9 f+ t. |3 P open cur_rows ( V6 A9 B7 M' Z2 x, t . r! t5 M/ {3 P0 ~% r0 ^/ efetch cur_rows into @id,@max' h$ t8 w# f+ ]: s ( [- b0 {* n9 A% X* O& hwhile @@fetch_status=02 j- s, w2 k: o " j2 d1 i* `8 p2 K* R begin' x9 C/ P. f3 P& b % \) f+ ~+ A5 Yselect @max = @max -1 ) a( W+ `) G- j3 U( M4 B 2 w3 ~8 `4 t8 l' ^0 mset rowcount @max % Q- \0 {, B( g+ I7 X& F/ z4 ^% `) P; S @9 `! K0 h: D, S& k delete from 表名 where 主字段 = @id) ?! J$ E4 `0 U6 Q4 w7 w z3 c ; n6 C: ^8 F5 e6 p0 i2 M7 v- P7 m6 k fetch cur_rows into @id,@max* S& n2 e% O, O& t( n0 V" t end( x% y2 t. I ^' h- |% L8 s 9 I: @- |0 }& Z+ Y" g+ G/ m2 Jclose cur_rows ' F' [9 n. E. t, h # h% ]6 i/ F& v! a8 n# o8 s! e) G, tset rowcount 0( M8 X6 ~% ~2 _6 k1 j$ ` 7 ^. s' b+ Y# S( R3 ^ 方法二 # s; e# R" v( A' V& u) E1 Z f7 n0 F8 |! W "重复记录"有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。 ; j- `) i; C. o6 x, @( U; B% [, x, ]+ @) K   1、对于第一种重复,比较容易解决,使用 + P, l, R2 d$ J, O+ ` R 7 |9 C, Y6 O( B+ v2 F, A& nselect distinct * from tableName 0 x6 {8 m8 i( w0 I4 ?) G & j6 r8 x. r/ f& ]* b9 `  就可以得到无重复记录的结果集。 6 a; K/ W6 E# o) Q2 m5 R8 D) v% [   如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除 " O; C% e; W# m3 ~$ l ; U$ D# z. {6 }5 d9 kselect distinct * into #Tmp from tableName" s7 [' L! a' h) t; P& K- x 3 |& _ ]- T! g! m3 u9 \9 p% @$ d drop table tableName , f/ g5 _) Z9 n4 W9 z# |, k) u5 e* c; G" G# X; A/ L% M select * into tableName from #Tmp * p( X/ S8 L4 t) g9 g6 U( g3 Y/ }, D0 s/ a5 R2 l select id from tablename . V3 ?; M3 h: ?' y& j 9 ^& f l7 V h5 R" Y( U' E3 qgroup by id % ^7 q9 V4 o1 s6 Q. d7 I. [( K6 V# o$ e* i6 i3 b having count(id) > 1 $ g7 x" Q- D' S1 l% r- C" O; J ) o/ s# W; g3 B6 p% [)
遨海湾-心灵的港湾 www.aosea.com
回复

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2025-1-18 15:56

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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