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

发布网友

我来回答

10个回答

热心网友

sql中删除一个表中的重复记录可以采用如下步骤: 

1、把a_dist表的记录用distinct去重,结果放到临时表中。

select  distinct * into #temp from a_dist;

2、把a_dist表的记录全部删除。

delete  from a_dist;

3、把临时表中的数据信息导进到a_dist表中,并删除临时表。

insert  into a_dist select * from #temp;

drop table #temp;

扩展资料:

SQL (结构化查询语言)是用于执行查询的语法。在数据库上执行的大部分工作都由 SQL 语句完成。SQL 语言包含用于更新、插入和删除记录的语法。

增删改查指令构成了 SQL 的 DML 部分:

热心网友

数据库去重复有以下三种方法:

1.两条记录或者多条记录的每一个字段值完全相同,这种情况去重复最简单,用关键字distinct就可以去掉。

2.两条记录之间之后只有部分字段的值是有重复的,但是表存在主键或者唯一性ID。如果是这种情况的话用DISTINCT是过滤不了的,这就要用到主键id的唯一性特点及group by分组。

3.两条记录之间之后只有部分字段的值是有重复的,但是表不存在主键或者唯一性ID。这种情况可以使用临时表,讲数据复制到临时表并添加一个自增长的ID,在删除重复数据之后再删除临时表。

扩展资料:

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、必须保证表中有主键或者唯一索引,或者某列数据不能重复。只有这样,才可能使用一句SQL来实现。否则只能考虑其它办法。下面的语句,假定BB列是不重复的,删除后保存BB列值最大的那条记录。
delete from 表
where aa in (select aa from 表 group by aa having count(aa) > 1) and
bb not in (select max(bb) from 表 group by aa having count(aa) > 1);

2、有多种写法:
delete A from B where A.AA = B.AA
delete A from A,B where A.AA = B.AA
delete A where AA in (select AA from B)

3、使用into关键字:
select * into 新表名 from 原表

4、取数据前3位,字段必须是类似char类型,使用类似substring这样的函数(SYBASE是substring,ORACLE是substr):
select substring(字段,1,3) from 表名

热心网友

可以给你个想法,把不重复的多出来放到一个临时表中,删除原表,再将临时表的数据插入原表

热心网友

如果记录完全相同才算重复记录,那么: (sql server2000下测试通过)

select distinct * into #tmpp from tid
delete from tid
insert into tid select * from #tmpp
drop table #tmpp

如果有id主键(数字,自增1的那种),那么:(sql server2000下测试通过)

delete from tableA where id not in
(select id = min(id) from tableA group by name)

热心网友

先删后加
delete from a_dist where id ='1' and name= 'abc' 执行删掉所有这样的记录,然后把数据记录下来在添加一次
insert into a_dist values(1,'abc');

热心网友

create view a_dist_view as
select a.*, row_number() over(order by id, name) rn from a_dist as a

delete from a_dist_view where rn <> 1追问能给解释解释思路吗,看不懂意思,row_numbenr(),over(order by id, name) rn 什么意思
where rn 1 什么意思

追答row_numbenr() over(order by id, name) rn
-- 这是一个数据库分析函数,含义是:按照id, name 排序(order by id, name),列出记录的序号。

where rn 1 什么意思
-- 如果id,name相同的话,rn会从1一直累加,所以如果要删除重复记录,只要保存rn=1的即可,所以删除 rn 1 的记录。

具体的含义如果还不清楚,你在机器上执行下sql就能明白了。

热心网友

delete 表 a wher rowid <>(max(rowid) from 表 b
where a.重复项=b.重复项 );

热心网友

全删吗?
delete from a_dist where id in(select id from a_dist group by id,name having count(*)<>1)

热心网友

还是跟着热心网友混生活吧。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com