1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
)
 例二:
 select * from testtable
 where numeber in (select number from people group by number having count(number) > 1 )
 可以查出testtable表中number相同的记录

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people 
)
)

3、查找表中多余的重复记录(多个字段) 
select * from vitae a
)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
)
)


5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a
)
)

(二)
比方说
在A表中存在一个字段“name”,
而且不同记录之间的“name”值有可能会相同,
现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;


如果还查性别也相同大则如下:



(三)
方法一

declare @max integer,@id integer



open cur_rows

fetch cur_rows into @id,@max



begin



set rowcount @max

delete from 表名 where 主字段 = @id

fetch cur_rows into @id,@max

end

close cur_rows



方法二

  有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。

1、对于第一种重复,比较容易解决,使用

select distinct * from tableName

就可以得到无重复记录的结果集。

如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除

select distinct * into #Tmp from tableName

drop table tableName

select * into tableName from #Tmp

drop table #Tmp

发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。

2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下

假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集

) as autoID, * into #Tmp from tableName

select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID

select * from #Tmp where autoID in(select autoID from #tmp2)

最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)

(四)
查询重复

select * from tablename where id in (

select id from tablename 

group by id 



)

SQL重复记录查询(转载)的更多相关文章

  1. sql重复记录查询

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select  peopleId  fro ...

  2. SQL重复记录查询的几种方法(转)

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 代码如下: select * from people ) 2.删除表中多余的重复记录,重复记录是根据单个字段(people ...

  3. 收藏:SQL重复记录查询 .

    来自:http://blog.csdn.net/chinmo/article/details/2184020 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select ...

  4. SQL重复记录查询-count与group by having结合查询重复记录

    查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select  peopleId  from  p ...

  5. SQL重复记录查询的几种方法

    http://www.jb51.net/article/34820.htm \ 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 复制代码 代码如下: select * f ...

  6. **SQL某一表中重复某一字段重复记录查询与处理

    sql某一表中重复某一字段重复记录查询与处理   1.查询出重复记录  select 重复记录字段 form  数据表 group by houseno having count(重复记录字段)> ...

  7. SQL重复记录处理(查找,过滤,删除)

    SQL重复记录处理(查找,过滤,删除)     ID int    Title nvarchar(50)    AddDate datetime    数据  www.2cto.com     ID ...

  8. sql 重复数据查询

    具体代码: ); ORDER BY tcount DESC;

  9. mysql 多个字段重复记录查询

    select * from productstockquantity t where () ORDER BY t.CombinationI

随机推荐

  1. Codeforces Round #190 (Div. 2) 水果俩水题

    后天考试,今天做题,我真佩服自己... 这次又只A俩水题... orz各路神犇... 话说这次模拟题挺多... 半个多小时把前面俩水题做完,然后卡C,和往常一样,题目看懂做不出来... A: 算是模拟 ...

  2. oracle 逗号分割,列转行,行转列

    SQL代码 列转行 select REGEXP_SUBSTR(a.rolecode ,,l) rolecode from ( select 'a,aa,aaa' rolecode from dual ...

  3. Class类中getMethods() 与getDeclaredMethods() 方法的区别

    一:jdk API中关于两个方法的解释 1:getMethods(),该方法是获取本类以及父类或者父接口中所有的公共方法(public修饰符修饰的) 2:getDeclaredMethods(),该方 ...

  4. LAMP平台搭建详解

    准备工作 安装编译工具 # yum -y install gcc # yum -y install gcc-c++ 如果系统之前已经安装有rpm包的mysql和apache,那么可以: #servic ...

  5. 24小时学通Linux内核之有关Linux文件系统实现的问题

    有时间睡懒觉了,却还是五点多醒了,不过一直躺倒九点多才算起来,昨晚一直在弄飞凌的嵌入式开发板,有些问题没解决,自己电脑系统的问题,虽然Win10发布了,,但我还是好喜欢XP呀,好想回家用用家里的XP来 ...

  6. 【数论-数位统计】UVa 11076 - Add Again

    Add AgainInput: Standard Input Output: Standard Output Summation of sequence of integers is always a ...

  7. 转: android编译过程(流程图)

  8. 关于oracle误删数据的恢复

    与数据打交道,免不了会误删一些数据,之后还commit了,连回滚的机会都没了,而更糟糕的是你又没有备份,这种事终于在今天被我不幸的遇上了... 唯一一点值得欣慰的是,我删除表记录的时候,时间不长,一天 ...

  9. Java Concurrency - 浅析 CyclicBarrier 的用法

    The Java concurrency API provides a synchronizing utility that allows the synchronization of two or ...

  10. MyBatis(3.2.3) - Mapped statements: The INSERT statement, Autogenerated keys

    We can use the useGeneratedKeys and keyProperty attributes to let the database generate the auto_inc ...