700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 用SQL语句删除重复记录的四种方法【MySQL】

用SQL语句删除重复记录的四种方法【MySQL】

时间:2019-09-28 01:52:55

相关推荐

用SQL语句删除重复记录的四种方法【MySQL】

数据库|mysql教程

,记录,如何

数据库-mysql教程

问题:如何把具有相同字段的记录删除,只留下一条。

内存修改器源码,ubuntu系统平板电脑,多足爬虫 图片,用php语言写php扩展,吉林手机seolzw

例如:表test里有id,name字段,如果有name相同的记录只留下一条,其余的删除。name的内容不定,相同的记录数不定。

酒店收银系统源码,vscode编辑keil,安装ubuntu后死机,tomcat过载保护,sqlite批量保存,下拉条美化插件,前端常用的css框架,爬虫出现401,php 学校,网站架构 seo,投资公司网站模板很大气,网页计算器 js代码下载,dz模板新锐,欢迎页面jsp源码,动易网站管理系统教程,网上购物系统软件源程序lzw

用SQL语句删除重复记录的四种方法:

方法1:

社区管理源码,vscode怎么降版本,ubuntu 分区 修改,tomcat放在什么位置,phton 爬虫,ubuntu16 php,湘潭网络seo优化方案,asp 全屏网站源码,网站电脑模板lzw

1、将重复的记录记入temp1表

select [标志字段id],count(*) into temp1 from [表名]group by [标志字段id]having count(*)>1

2、将不重复的记录记入temp1表

insert temp1select [标志字段id],count(*) from [表名]group by [标志字段id]having count(*)=1

3、作一个包含所有不重复记录的表

select * into temp2 from [表名]where 标志字段id in(select 标志字段id from temp1)

4、删除重复表:delete [表名]

5、恢复表

insert [表名]select * from temp2

6、删除临时表

drop table temp1drop table temp2

方法2:

declare @max integer,@id integerdeclare cur_rows cursor local for select id,count(*) from 表名 group by id having count(*) > 1open cur_rowsfetch cur_rows into @id,@maxwhile @@fetch_status=0beginselect @max = @max -1set rowcount @maxdelete from 表名 where id = @idfetch cur_rows into @id,@maxendclose cur_rowsset rowcount 0

注:set rowcount @max – 1表示当前缓冲区只容纳@max-1条记录,如果有十条重复的,就删除10条,一定会留一条的。也可以写成delete from 表名。

方法3:

create table a_dist(id int,name varchar(20))insert into a_dist values(1,’abc’)insert into a_dist values(1,’abc’)insert into a_dist values(1,’abc’)insert into a_dist values(1,’abc’)exec up_distinct ‘a_dist’,’id’select * from a_distcreate procedure up_distinct(@t_name varchar(30),@f_key varchar(30))–f_key表示是分组字段﹐即主键字段asbegindeclare @max integer,@id varchar(30) ,@sql varchar(7999) ,@type integerselect @sql = ‘declare cur_rows cursor for select ‘+@f_key+’ ,count(*) from ‘ +@t_name +’ group by ‘ +@f_key +’ having count(*) > 1’exec(@sql)open cur_rows fetch cur_rows into @id,@max while @@fetch_status=0 begin select @max = @max -1 set rowcount @max select @type = xtype from syscolumns where id=object_id(@t_name) and name=@f_keyif @type=56select @sql = ‘delete from ‘+@t_name+’ where ‘ + @f_key+’ = ‘+ @id if @type=167select @sql = ‘delete from ‘+@t_name+’ where ‘ + @f_key+’ = ‘+””+ @id +”” exec(@sql)fetch cur_rows into @id,@max end close cur_rows deallocate cur_rowsset rowcount 0endselect * from systypesselect * from syscolumns where id = object_id(‘a_dist’)

方法4:

可以用IGNORE_DUP_KEY: create table dup (id int identity not null,name varchar(50)not null)goinsert into dup(name) values (‘abc’)insert into dup(name) values (‘abc’)insert into dup(name) values (‘abc’)insert into dup(name) values (‘abc’)insert into dup(name) values (‘abc’)insert into dup(name) values (‘abc’)insert into dup(name) values (‘abc’)insert into dup(name) values (‘cdefg’)insert into dup(name) values (‘xyz’)insert into dup(name) values (‘xyz’)goselect *from dupgocreate table tempdb..wk(id int not null, name varchar(50)not null)gocreate unique index idx_remove_dup on tempdb..wk(name)with IGNORE_DUP_KEY goINSERT INTO tempdb..wk (id, name)select id, namefrom dupgoselect *from tempdb..wkgodelete from dupgoset identity_insert dup onINSERT INTO dup (id, name)select id, namefrom tempdb..wkgoset identity_insert dup offgoselect *from dupgo

注释:此处delete原表,再加入不重复的值。大家也可以通过join只delete原表中重复的值

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。