用SQL语句删除重复记录的四种方法

时间:2011-05-03 09:25:00 

问题:如何把具有相同字段的记录删除,只留下一条。   例如:表test里有id,name字段,如果有name相同的记录只留下一条,其余的删除。name的内容不定,相同的记录数不定。   用SQL语句删除重复记录的四种方法:


方法1:   1、将重复的记录记入temp1表  


select [标志字段id],count(*) into temp1 from [表名]group by [标志字段id]having count(*)>12、将不重复的记录记入temp1表  


insert temp1select [标志字段id],count(*) from [表名]group by [标志字段id]having count(*)=1  3、作一个包含所有不重复记录的表  


select * into temp2 from [表名]where 标志字段id in(select 标志字段id from temp1)  4、删除重复表:delete [表名]   5、恢复表


insert [表名]select * from temp2

6、删除临时表


drop table temp1drop table temp2  方法2:




declare @max integer,@id integer
declare cur_rows cursor local for
select id,count(*) from 表名 group by id having count(*) > 1
open cur_rows
fetch cur_rows into @id,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
delete from 表名 where id = @id
fetch cur_rows into @id,@max
end
close cur_rows
set rowcount 0

注:set rowcount @max - 1表示当前缓冲区只容纳@max-1条记录,如果有十条重复的,就刪除10条,一定会留一条的。也可以写成delete from 表名。   方法3:  




create table a_dist(id int,name varchar(20))

insert into a_dist values(1,'abc')
insert into a_dist values(1,'abc')
insert into a_dist values(1,'abc')
insert into a_dist values(1,'abc')exec up_distinct 'a_dist','id'select * from a_distcreate procedure up_distinct(@t_name varchar(30)
,@f_key varchar(30))
--f_key表示是分组字段﹐即主键字段
as
begin
declare @max integer,@id varchar(30) ,
@sql varchar(7999) ,@type integer
select @sql = 'declare cur_rows cursor
for select '+@f_key+' ,count(*) from '
+@t_name +' group by ' +@f_key +' having count(*) > 1'
exec(@sql)
open cur_rows
fetch cur_rows into @id,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
select @type = xtype from syscolumns
where id=object_id(@t_name) and name=@f_key
if @type=56
select @sql = 'delete from '+@t_name+'
where ' + @f_key+' = '+ @id
if @type=167
select @sql = 'delete from '+@t_name+'
where ' + @f_key+' = '+''''+ @id +''''
exec(@sql)
fetch cur_rows into @id,@max
end
close cur_rows
deallocate cur_rows
set rowcount 0
endselect * from systypes
select * from syscolumns where
id = object_id('a_dist')

方法4:

可以用IGNORE_DUP_KEY:


create table dup (id int identity not null,
name varchar(50)not null)
go
insert into dup(name) values ('abc')
insert into dup(name) values ('abc')
insert into dup(name) values ('abc')
insert into dup(name) values ('abc')
insert into dup(name) values ('abc')
insert into dup(name) values ('abc')
insert into dup(name) values ('abc')
insert into dup(name) values ('cdefg')
insert into dup(name) values ('xyz')
insert into dup(name) values ('xyz')
go
select *
from dup
go
create table tempdb..wk(id int not null,
name varchar(50)not null)
go
create unique index idx_remove_dup
on tempdb..wk(name)
with IGNORE_DUP_KEY
go
INSERT INTO tempdb..wk (id, name)
select id, name
from dup
go
select *
from tempdb..wk
go
delete from dup
go
set identity_insert dup onINSERT INTO dup (id, name)
select id, name
from tempdb..wk
go
set identity_insert dup off
go
select *
from dupgo

  注释:此处delete原表,再加入不重复的值。大家也可以通过join只delete原表中重复的值。

标签:重复记录,sql
0
投稿

猜你喜欢

  • SQL Server 2000数据库FOR XML查询概述

    2008-12-09 14:49:00
  • 如何防范利用刷新来“作弊”的计数器?

    2009-11-22 17:18:00
  • 交互设计实用指南系列(2) –可及

    2010-01-21 12:48:00
  • Perl下应当如何连接Access数据库

    2008-12-04 13:06:00
  • 设计上的小细节

    2010-06-24 21:44:00
  • 数据库复制性能测试 推送模式性能测试

    2012-07-11 16:13:52
  • javascript面向对象技术基础(一)

    2010-02-07 13:01:00
  • js查找/搜索页面中的字符源代码

    2008-02-17 17:13:00
  • CSS.JS文件发布机制的思考

    2009-08-04 13:07:00
  • Oracle RMAN快速入门指南

    2010-07-18 12:50:00
  • asp 采集程序常用函数分析

    2011-03-16 11:03:00
  • 介绍27款经典的CSS框架

    2011-03-04 16:24:00
  • Flash真的适合做网站应用吗?

    2011-04-16 10:34:00
  • SQL Server 2005返回刚刚插入的数据条目id值

    2008-12-04 17:16:00
  • QQ连连看 for iPhone 设计实录分享

    2010-09-25 12:54:00
  • 在SQL Server 2008中安装安全审计

    2009-02-24 17:22:00
  • mysql 错误:ERROR 1045 (28000): Access deni

    2010-09-30 14:48:00
  • 防采集,几种觉得有用的防采集方法

    2009-09-03 13:30:00
  • MySQL的双机热备份安装和配置

    2012-01-05 19:00:58
  • 一个asp分页源代码例子

    2007-09-19 12:25:00
  • asp之家 网络编程 m.aspxhome.com