在大的数据库应用中,经常因为各种原因遇到重复的记录,造成数据的冗余和维护上的不便。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;
insert into A (Field1、Field2) select B.Field1,B.Field2 from
A,B where A.Field1<>B.Field1
http://community.csdn.net/Expert/topic/4271/4271982.xml?temp=.8837397http://community.csdn.net/Expert/topic/4224/4224385.xml?temp=.6532556
RowId 与 Groupby 哪一种快一点?
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
go
不用create 了,第一个语句就会自动创建此临时表 #
delete #1 where field1 in(select field1 from a)
insert a select field1,min(field2) field2 from #1 group by field1
的方法会不会有问题呢?
比如B的表中有重复数据,但A中没有这些数据,用Zlp的方法会不会把重复数据插入,
然后因为主键冲突而失败呢?
请问老大,怎样用sql语句删除字段sid,然后再插入?
因为表A没有sid字段。
而且表B字段很多,select 语句会写道手软。
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