删除重复记录,并且剩下一条

本文详细介绍了如何使用SQL语句删除数据库中重复的记录,包括使用ROW_ID和多个字段组合来识别重复项,并提供了具体的操作步骤和代码示例。适用于处理数千条记录的数据库优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据库中有若干条相同的记录,删除到只剩下一条记录。如何做,请用SQL语句通过ROW_ID来写出执行过程。

Row_id表示的是一个记录的物理存储地址。

Delete table_name where ziduan=’’ and rowid<(select max(rowid) from table_name where ziduan=’’);

 删除重复字段

采用的SQL语句:
Delete from TempA where id not in (
        select max(t1.id) from TempA t1 group by
         t1.PositionName,t1.EnglishPositionName)

在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢?
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select   *   from   people
where   peopleId   in   (select     peopleId     from     people     group     by     peopleId     having     count(peopleId)   >   1)

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete   from   people  
where   peopleId     in   (select     peopleId     from   people     group     by     peopleId       having     count(peopleId)   >   1)
and   rowid   not   in   (select   min(rowid)   from     people     group   by   peopleId     having   count(peopleId   )> 1)

3、查找表中多余的重复记录(多个字段)  
select   *   from   vitae   a
where   (a.peopleId,a.seq)   in     (select   peopleId,seq   from   vitae   group   by   peopleId,seq     having   count(*)   >   1)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete   from   vitae   a
where   (a.peopleId,a.seq)   in     (select   peopleId,seq   from   vitae   group   by   peopleId,seq   having   count(*)   >   1)
and   rowid   not   in   (select   min(rowid)   from   vitae   group   by   peopleId,seq   having   count(*)> 1)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select   *   from   vitae   a
where   (a.peopleId,a.seq)   in     (select   peopleId,seq   from   vitae   group   by   peopleId,seq   having   count(*)   >   1)
and   rowid   not   in   (select   min(rowid)   from   vitae   group   by   peopleId,seq   having   count(*)> 1)

 

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

如果还查性别也相同大则如下:
Select   Name,sex,Count(*)   From   A   Group   By   Name,sex   Having   Count(*)   >   1
------------------------------------------------------------------------------------------------
declare   @max   integer,@id   integer
declare   cur_rows   cursor   local   for   select   主字段,count(*)   from   表名   group   by   主字段   having   count(*)   > ;   1
open   cur_rows
fetch   cur_rows   into   @id,@max
while   @@fetch_status=0
begin
select   @max   =   @max   -1
set   rowcount   @max
delete   from   表名   where   主字段   =   @id
fetch   cur_rows   into   @id,@max
end
close   cur_rows
set   rowcount   0

方法二
  有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如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,要求得到这两个字段唯一的结果集
select   identity(int,1,1)   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 
having   count(id)   >   1)

 

 

如果只是要查询不是重复的记录的话
select distinct * from 表
如果是将多余的重复记录删除
首先将不是重复的数据提取出来,保存到一个临时表中
select distinct * into #temp from 表
然后删除原来的表
delete from 表
最后往里面插入临时表的数据
insert into 表
select * from #temp

 

 

这个问题比较老了,可以这样:
1,如果是对于个别字段:
SELECT   *   from   t,
        (select   m,n   from   t   b   group   by   m,n   having   count(*)> 1)   t1
where     t.m=t1.m   and   t.n=t1.n
2,对于所有字段:
--转移不重复的数据到临时表
select   distinct   *   into   #tmp   from   tb
/*
删除原表所有数据,个人认为,此处用truncate   table   ,而不用drop   table,原因是如果用drop   table之后,在重新创建该表时,得到的表中的有关字段的一些信息可能会和以前的不一样。
*/
truncate   table   tb
--插入不重复数据到原表
insert   tb   select   *   from   #tmp
--删除临时表
drop   table   *tmp

 

---查找重复记录
select   *   from   table1   group   by   username   having   count(*)> 1

-----删除重复记录

delete   from   table1   where   name   not   in   (select   max(name)   from   table1   )

delete   from   table1   where   name   not   in   (select   min(name)   from   table1   )

转载于:https://www.cnblogs.com/zousheng7018/archive/2012/10/23/2735061.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值