标签:
1.重复记录可由单个字段来辨别出:id为1和id为6的姓名一样
select * from student where name in (select name from student group by name having COUNT(name)>1)
in操作符:允许在where子句中规定多个值;group by:将结果集按name分组;having:WHERE 关键字无法与合计函数一起使用;
delete from student where name in (select name from student group by name having COUNT(name)>1)
2.重复记录所有字段都相同:即id,name,grade的值都相同
select distinct * into #Tmp from Table_1
drop table Table_1
select * into Table_1 from #Tmp
drop table #Tmp
#Tmp:临时表
临时表存储在 tempdb 中,当不再使用时会自动删除。
临时表有两种类型:本地和全局。它们在名称、可见性以及可用性上有区别。本地临时表的名称以单个数字符号 (#) 打头;它们仅对当前的用户连接是可见的;当用户从 SQL Server 实例断开连接时被删除。全局临时表的名称以两个数字符号 (##) 打头,创建后对任何用户都是可见的,当所有引用该表的用户从 SQL Server 断开连接时被删除。
发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。
3.部分关键字段重复的记录:即name和grade的值相同
select IDENTITY(int,1,1) as autoID,* into #Tmp from student1
select MIN(autoID) as autoID into #tmp2 from #Tmp group by name,grade
select * from #Tmp where autoID in(select autoID from #tmp2)
标签:
原文地址:http://www.cnblogs.com/xixifuxixi/p/4643118.html