武汉网站建设
关于三英 | 联系我们 | 网站地图 | 付款方式 | 帮助中心       
武汉IDC数据中心
咨询电话:027-63180773 13797060258
网络推广
企业邮箱
经典案例
联系我们
 
 
服务中心
快速服务通道
武汉网站制作设计开发 我想要建网站
网站模块 我想增加现有网站的功能
网站推广 推广您的网站
武汉域名注册 注册一个自己的域名
武汉虚拟主机 看一下适合我的虚拟主机
武汉企业邮局 我想要一个公司企业邮局
服务中心
google
百度
  您现在的位置:首 页 >> 数据库常见问题 >> 详情

 

如何删除表中的重复记录?

时间:2008/5/8 来源:武汉三英网站建设 浏览:25次

我们先从以下例子来分析
--测试数据
/*-----------------------------
select * from tt
-----------------------------*/
id          pid        
----------- -----------
1           1
1           1
2           2
3           3
3           3
3           3

(所影响的行数为 6 行)

首先,如何查询table中有重复记录
select *,count(1) as rownum
from tt
group by id, pid
having count(1) > 1
id          pid         rownum     
----------- ----------- -----------
1           1           2
3           3           3

(所影响的行数为 2 行)

方法一:使用distinct和临时表
if object_id('tempdb..#tmp') is not null
drop table #tmp
select distinct * into #tmp from tt
truncate table tt
insert into tt select * from #tmp
方法二:添加标识列
alter table tt add NewID int identity(1,1)
go 
delete from tt  where exists(select 1 from tt a where  a.newid>tt.newid and tt.id=a.id and tt.pid=a.pid)
go
alter table tt drop column NewID
go

--测试结果
/*-----------------------------
select * from tt
-----------------------------*/
id          pid        
----------- -----------
1           1
2           2
3           3

(所影响的行数为 3 行)

本站关键字:武汉网站建设,武汉网站报价,武汉网站设计,武汉网站制作
Copyright 2008 027-8.com All Rights Reserved
版权所有 武汉三英网站建设 服务电话:027-63180773 13797060258 企业邮局
信息产业部网站备案号:鄂ICP备06023403号    QQ交谈 酷站目录SEEK114分类网站目录