首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么删除重复的记录

2012-01-07 
如何删除重复的记录创建的时候没加约束条件,插入的时候失误,把成绩插入了2遍,如何删除重复的啊?[解决办法]

如何删除重复的记录
创建的时候没加约束条件,插入的时候失误,把成绩插入了2遍,
如何删除重复的啊?

[解决办法]
select distinct * into #tmp from 表
delete from 表
insert into 表 select * from #tmp
drop table #tmp
[解决办法]
select distinct * into #tmp from 表 
GO
delete from 表 
GO
insert into 表 select * from #tmp 
GO
drop table #tmp
GO
[解决办法]
alter table table_name add id int identity(1,1)

delete from table_name where id not in(select min(id) from table_name )

alter table table_name drop column id
[解决办法]
没有自增的处理方法:

列名:col1,col2重复
alter table t1 add row int identity(1,1)--增列标识列
go
delete t from t1 t where exists(select 1 from t1 where col1=t.col1 and col2=t.col2 and row>t.row)

delete t1 where row not in(select max(row) from t1 group by checksum(col1,col2))
--保留后一条
delete t from t1 t where exists(select 1 from t1 where col1=t.col1 and col2=t.col2 and row<t.row)

delete t1 where row not in(select min(row) from t1 group by checksum(col1,col2))
--保留前一条

热点排行