Sql 删除重复记录没有大小关系时,重复值将如何处理呢? 下文就将为您介绍sql删除重复记录没有大小关系时重复值的处理方法,供您参考,希望对您有所启迪。
–> –> (roy)生成
if not object_id(tempdb..#t) is nulldrop table #tgocreate table #t([num] int,[name] nvarchar(1))insert #tselect 1,na union allselect 1,na union allselect 1,na union allselect 2,nb union allselect 2,nbgo方法1:
if object_id(tempdb..#) is not nulldrop table #select distinct * into # from #t–排除重复记录结果集生成临时表#
truncate table #t–清空表
insert #t select * from # –把临时表#插入到表#t中–查看结果select * from #t
/*num name———– —-1 a2 b
(2 行受影响)#p#*/
–重新执行测试数据后用方法2方法2:
alter table #t add id int identity–新增标识列godelete a from #t a where exists(select 1 from #t where num=a.num and name=a.name and id>a.id)–只保留一条记录goalter table #t drop column id–删除标识列–查看结果select * from #t

/*num name———– —-1 a2 b
(2 行受影响)
–重新执行测试数据后用方法3方法3:
declare roy_cursor cursor local for#p#select count(1)-1,num,name from #t group by num,name having count(1)>1declare @con int,@num int,@name nvarchar(1)open roy_cursorfetch next from roy_cursor into @con,@num,@namewhile @@fetch_status=0beginset rowcount @con;delete #t where num=@num and name=@nameset rowcount 0;fetch next from roy_cursor into @con,@num,@nameendclose roy_cursordeallocate roy_cursor–查看结果select * from #t/*num name———– —-1 a2 b
(2 行受影响)*///利用存储过程
declare @max integer,@id integerdeclare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) > 1open cur_rowsfetch cur_rows into @id,@maxwhile @@fetch_status=0beginselect @max = @max -1set rowcount @maxdelete from 表名 where 主字段 = @IDFetch cur_rows into @id,@maxendclose cur_rowsset rowcount 0//使用函数
select distinct * into #tmp from tablenamedrop table tablenameselect * into tablename from #tmpdrop table #tmp
sql中删除重复数据
图片很模糊看不清,你看看下面方法可以吗
去除表A的重复值
user MyDB --进入需要修改的数据库中select distinct * into #tmp from [A] --先将数据存入一张临时表中,剔除重复项truncate table [A] --然后清空原表insert into [A] select * from #tmp --再将临时表内容插入表Adrop table #tmp --删除临时表
你的COl001 是一样的啊,你表里有主键码,或者自增字段?
你表里不算有重复值的啊,虽然前边一样,后边字段是不一样的,这样还算重复?
sql server数据库表中如何根据某个字段删除重复数据?
我用游标实现了你的功能。 你首先建立一张空表,和你的操作表一样的结构,但是要求是空表,没有任何内容,比如是tempReg2你把下面的代码拷贝到SQL查询分析器,稍作修改就行。 ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※DECLARE Cursor_Title CURSOR FOR SELECT distinct title FROM RegMemberOPEN Cursor_Titledeclare @str varchar(50)FETCH NEXT FROM Cursor_Title Into @strWHILE @@FETCH_STATUS = 0BEGIN insert into tempReg2 select top 1 * from RegMember where title=@str FETCH NEXT FROM Cursor_Title Into @strENDCLOSE Cursor_TitleDEALLOCATE Cursor_Title※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※我用的表名是RegMember,重复的列名是title,所以这两个名称需要你替换一下。 别的可以不变。
mysql怎么去除重复数据
//去除重复的数据 如果是重复的只显示1个select distinct d_name from tbl_vod--下面是查出重复的记录select d_name from tbl_vodminusselect distinct d_name from tbl_vod
发表评论