怎样消除表中的重复记录
表A 及 表B 有完全相同的结构。两个字段Field1、Field2
表A中Field1是主键,表B中没有主键。
现在要把表B中的记录插入表A中。
我现在的做法是:
------------------------------------------------------------
insert into a
select * from b
where field1 in
(select field1 from b group by field1 having count(field1)<2)
delete from b where field1 in
(select field1 from b group by field1 having count(field1)<2)
declare @f1 int
while exists (select top 1 field1 from b )
begin
select top 1 @f1=field1 from b
insert into a select top 1 * from b where field1=@f1
delete from b where field1=@f1
end
----------------------------------------------------------------------
当表B数据量很大时,速度非常慢。
请各位大侠帮我修改修改,给个简单快速的方法,重酬。!
问题点数:100、回复次数:15Top
1 楼lizhaogui()回复于 2005-09-16 12:44:52 得分 10
在大的数据库应用中,经常因为各种原因遇到重复的记录,造成数据的冗余和维护上的不便。
1.用rowid方法
2.用group by方法
3.用distinct方法
1。用rowid方法
据据oracle带的rowid属性,进行判断,是否存在重复,语句如下:
查数据:
select * from table1 a where rowid !=(select max(rowid)
from table1 b where a.name1=b.name1 and a.name2=b.name2......)
删数据:
delete from table1 a where rowid !=(select max(rowid)
from table1 b where a.name1=b.name1 and a.name2=b.name2......)
2.group by方法
查数据:
select count(num), max(name) from student --列出重复的记录数,并列出他的name属性
group by num
having count(num) >1 --按num分组后找出表中num列重复,即出现次数大于一次
删数据:
delete from student
group by num
having count(num) >1
这样的话就把所有重复的都删除了。
3.用distinct方法 -对于小的表比较有用
create table table_new as select distinct * from table1 minux
truncate table table1;
insert into table1 select * from table_new;
Top
2 楼zlp321002(Life Is Good,Let's Shine)回复于 2005-09-16 12:48:51 得分 10
--try
insert into A (Field1、Field2) select B.Field1,B.Field2 from
A,B where A.Field1<>B.Field1
Top
3 楼lisiyong(小样)回复于 2005-09-16 12:49:58 得分 10
可以看看:
http://community.csdn.net/Expert/topic/4271/4271982.xml?temp=.8837397
http://community.csdn.net/Expert/topic/4224/4224385.xml?temp=.6532556Top
4 楼JimmyBoy(JimmyBoy)回复于 2005-09-16 12:53:07 得分 0
谢谢楼上。
RowId 与 Groupby 哪一种快一点?Top
5 楼wgsasd311(自强不息)回复于 2005-09-16 12:55:39 得分 0
先顶下,再回答。Top
6 楼wgsasd311(自强不息)回复于 2005-09-16 12:58:30 得分 0
select *,identity(int,1,1) as sid into # from B
go
delete a from # a,# b where a.field1=b.field1 and a.sid>b.sid
go
insert into A(filed1,field2)
select filed1,field2 from #
go
drop table #,B
goTop
7 楼wgsasd311(自强不息)回复于 2005-09-16 13:00:57 得分 0
我个人觉得:zlp321002(人生没有理想,那和咸鱼还有什么两样) 的方法最好,因为你的A表中field1是主键,所以没必要那么麻烦,直接两表相连插入。Top
8 楼JimmyBoy(JimmyBoy)回复于 2005-09-16 13:05:49 得分 0
wgsasd311老大的答复我看不懂,#,# a,# b 是什么?不用先Create吗?Top
9 楼JimmyBoy(JimmyBoy)回复于 2005-09-16 13:10:54 得分 0
现在懂了。
谢楼上各位,再看看,然后给分。Top
10 楼wgsasd311(自强不息)回复于 2005-09-16 13:11:46 得分 0
wgsasd311老大的答复我看不懂,#,# a,# b 是什么?不用先Create吗?----------# 表示一个临时表 # a,# b 分别表示同一个表(#),取了两个不同的别名 (a,b)
不用create 了,第一个语句就会自动创建此临时表 #Top
11 楼xueguang(xg)回复于 2005-09-16 13:12:27 得分 10
select * into #1 from b
delete #1 where field1 in(select field1 from a)
insert a select field1,min(field2) field2 from #1 group by field1Top
12 楼JimmyBoy(JimmyBoy)回复于 2005-09-16 13:14:20 得分 0
zlp321002(人生没有理想,那和咸鱼还有什么两样)
的方法会不会有问题呢?
比如B的表中有重复数据,但A中没有这些数据,用Zlp的方法会不会把重复数据插入,
然后因为主键冲突而失败呢?
Top
13 楼JimmyBoy(JimmyBoy)回复于 2005-09-16 13:19:05 得分 0
用自强不息的方法:
请问老大,怎样用sql语句删除字段sid,然后再插入?
因为表A没有sid字段。
而且表B字段很多,select 语句会写道手软。
Top
14 楼wgsasd311(自强不息)回复于 2005-09-16 13:26:11 得分 60
select *,identity(int,1,1) as sid into # from B
go
delete a from # a,# b where a.field1=b.field1 and a.sid>b.sid
go
alter table # drop column sid
go
insert into A
select *from #
go
drop table #,B
go
Top
15 楼wgsasd311(自强不息)回复于 2005-09-16 13:27:07 得分 0
alter table # drop column sid ---删除字段 sidTop




