栏目导航
热点推荐
- 服务器安全配置:常用的WEB服务器
- Linux新手要了解的十个知识点
- Windows Xp下配置IIS的简单教程
- XP配置IIS+ASP详解(新手调试ASP
- Window2003配置ASP程序2个常见问
- Linux下禁止别人ping
- 收集IIS配置错误-- 您未被授权查
- Linux设备文件简介
- 如何修复Linux文件系统错误
- Linux服务器安全之四种攻击级别
- windows 2000服务器IIS开启父路
- IIS无法启动错误的几种解决方法
阅览排行
SQL删除重复记录(针对于某几个字段相同)
www.jz123.cn 2011-06-23 来源: 站长家园会员投稿 南京互易中国 我要投递新闻
例:表中有条六条记录。 其中张三和王五 的记录有重复
TableA
id customer PhoneNo
001 张三 777777
002 李四 444444
003 王五 555555
004 张三 777777
005 张三 777777
006 王五 555555
如何写一个sql语句将TableA变成如下
001 张三 777777
002 李四 444444
003 王五 555555
--测试环境
create table TableA ( id varchar(3),customer varchar(5),PhoneNo varchar(6))
insert into TableA select '001','张三','777777'
union all select '002','李四','444444'
union all select '003','王五','555555'
union all select '004','张三','777777'
union all select '005','张三','777777'
union all select '006','王五','555555'
--结果
delete TableA from TableA T where
exists(
select 1 from tablea where customer=T.customer and phoneno=T.phoneno
and id < tt.id
)
--总结
该方法适用于有一个字段为自增性,例如本例中的:id
delete 表名 from 表名 as T where
exists(
select 1 from 表名 where 字段A=T.字段A and 字段B=T.字段B,(....)
and 自增列 < T.自增列
)
上一篇:SQL Server 2008 R2中的新组件特性 下一篇:放弃Windows 转投Linux的十个理由