--数据库的使用过程中由于程序方面的问题有时候会碰到重复数据,重复数据导致了数据库部分设置不能正确设置……
方法一
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,Address select * from #Tmp where autoID in(select autoID from #tmp2)
最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)
在导入到原数据表之前,我们需要将删除新添加的autoID字段列,保证数据表之间字段结构的一致.
再创建一个中间表Tmp3,将上面最后一部获得的不重复的结果集导入到Tmp3之中.
select * into #Tmp3 from #Tmp where autoID in (select autoID from #Tmp where autoID in(select autoID from #tmp2))
这样,Tmp3中保存的记录集合就是我们所希望得到的不重复的结果集.但由于Tmp3中存在autoID字段,所以我们先删除Tmp3表中该列。
Alter TableName Drop Column autoID
接着我们采取第一种重复的解决方法,将Tmp3中的记录集合导入到我们所需要导入的数据表之中.
select distinct * into #Tmp3 from tableNamedrop table tableNameselect * into tableName from #Tmp3drop table #Tmp3drop table #Tmp2drop table #Tmp
通过以上步骤,tableName中就是我们想要得到的结果集,不存在重复的记录
本文出自 “孤单” 博客,转载请与作者联系!
PL/SQL删除重复记录
标签:pl/sql删除重复记录
小编还为您整理了以下内容,可能对您也有帮助:
sql中如何删除一个表中重复的记录?
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 部分:
SELECT - 从数据库表中获取数据
UPDATE - 更新数据库表中的数据
DELETE - 从数据库表中删除数据
INSERT INTO - 向数据库表中插入数据
plsql删除表中相同数据
删除表中多余的重复记录(多个字段),只留有rowid最小的记录
假设字段为a,b,c
delete from 表 aa where (aa.a,aa.b,aa.c) in (select a,b,c from 表 group by a,b,c having count(*) > 1) and rowid not in (select min(rowid) from 表 group by a,b,c having count(*)>1)
plsql删除表中相同数据
删除表中多余的重复记录(多个字段),只留有rowid最小的记录
假设字段为a,b,c
delete from 表 aa where (aa.a,aa.b,aa.c) in (select a,b,c from 表 group by a,b,c having count(*) > 1) and rowid not in (select min(rowid) from 表 group by a,b,c having count(*)>1)
plsql名称已由现有对象使用在哪里找到重复对象删除
1、首先列出某个字段重复的所有行。
2、删除重复数据,留rowid最小的行。
如何用SQL语句删除两个表中相同的记录?
1,首先创建一个表,并在表中插入重复的记录,如下图所示。
2,插入好以后就看见表中已经有重复的数据了,如下图所示。
3,接下来在删除之前我们记得一定先备份,如下图所示。
4,然后排除重复的记录可以通过distinct字段设置,如下图所示,然后将去重的数据插入到新表中。
5,接着看到数据表下面多出来一个刚建的新表,如下图所示。
6,最后打开新表,就可以看到重复的数据都没有了,如下图所示。
如何用SQL语句删除两个表中相同的记录?
1,首先创建一个表,并在表中插入重复的记录,如下图所示。
2,插入好以后就看见表中已经有重复的数据了,如下图所示。
3,接下来在删除之前我们记得一定先备份,如下图所示。
4,然后排除重复的记录可以通过distinct字段设置,如下图所示,然后将去重的数据插入到新表中。
5,接着看到数据表下面多出来一个刚建的新表,如下图所示。
6,最后打开新表,就可以看到重复的数据都没有了,如下图所示。
sql查询去掉重复记录
1、打开要去掉重复数据的数据库,这里新建一张含有重复数据的user表做示例,如下图所示:
2、输入“select * from user where name in (select name from user group by name having count(name) > 1) ”sql语句,点击运行可以看到查询出了数据库中user表的重复数据。
3、通过“delete from user where name in (select name from user group by name having count(name) > 1) ”sql语句删除姓名重复的数据。
4、也可以通过“select distinct name from user”sql语句来去掉重复数据,这里去掉了张三的重复数据。
5、通过“select distinct class from user”sql语句来去掉班级相同的重复数据,如下图所示:
sql如何删除重复数据?
sql查询去除重复值语句
sql 单表/多表查询去除重复记录
单表distinct
多表group by
group by 必须放在 order by 和 limit之前,不然会报错
************************************************************************************
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(*)>
sql如何删除重复数据?
sql查询去除重复值语句
sql 单表/多表查询去除重复记录
单表distinct
多表group by
group by 必须放在 order by 和 limit之前,不然会报错
************************************************************************************
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(*)>
PLSQL执行脚本时怎么去除重复的数据
insert into T(AID,BID,……)
SELECT AID,BID,……
FROM T1
WHERE NOT EXISTS (SELECT 1 FROM T AID=T1.AID AND BID=T1.BID)
PLSQL执行脚本时怎么去除重复的数据
insert into T(AID,BID,……)
SELECT AID,BID,……
FROM T1
WHERE NOT EXISTS (SELECT 1 FROM T AID=T1.AID AND BID=T1.BID)
sql数据库中出现重复行数据,如何删除这些重复记录?
假设存在一个产品信息表Procts,其表结构如下:
CREATE TABLE Procts (ProctID int,
ProctName nvarchar (40),
Unit char(2),
UnitPrice money
)
表中数据如图:
图中可以看出,产品Chang和Tofu的记录在产品信息表中存在重复。现在要删除这些重复的记录,只保留其中的一条。步骤如下:
第一步——建立一张具有相同结构的临时表
CREATE TABLE Procts_temp (ProctID int,
ProctName nvarchar (40),
Unit char(2),
UnitPrice money
)
第二步——为该表加上索引,并使其忽略重复的值
方法是在企业管理器中找到上面建立的临时表Procts _temp,单击鼠标右键,选择所有任务,选择管理索引,选择新建。如图2所示。
按照图2中圈出来的地方设置索引选项
第三步——拷贝产品信息到临时表
insert into Procts_temp Select * from Procts此时SQL Server会返回如下提示:
服务器: 消息 3604,级别 16,状态 1,行 1
已忽略重复的键。
它表明在产品信息临时表Procts_temp中不会有重复的行出现。
第四步——将新的数据导入原表
将原产品信息表Procts清空,并将临时表Procts_temp中数据导入,最后删除临时表Procts_temp。
delete Procts insert into Procts select * from Procts_temp drop table Procts_temp
这样就完成了对表中重复记录的删除。无论表有多大,它的执行速度都是相当快的,而且因为几乎不用写语句,所以它也是很安全的
sql中删除重复数据
SQL Server删除重复行是我们最常见的操作之一,下面就为您介绍六种适合不同情况的SQL Server删除重复行的方法,供您参考。
1.如果有ID字段,就是具有唯一性的字段
delect table where id not in (
select max(id) from table group by col1,col2,col3...
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。
2. 如果是判断所有字段也可以这样
select * into #aa from table group by id1,id2,....
delete table
insert into table
select * from #aa
3. 没有ID的情况
select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp
4. col1+','+col2+','...col5 联合主键
select * from table where col1+','+col2+','...col5 in (
select max(col1+','+col2+','...col5) from table
where having count(*)>1
group by col1,col2,col3,col4
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。
5.
select identity(int,1,1) as id,* into #temp from tabel
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)
6.
select distinct * into #temp from tablename
delete tablename
go
insert tablename select * from #temp Sqlclub
go
drop table #temp
以上就是SQL Server删除重复行的方法介绍。