用SQL语句,删除掉重复项只保留一条

原文 用SQL语句,删除掉重复项只保留一条

在几千条记录里,存在着些相同的记录,如何能用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   peopleName in (select peopleName    from people group by peopleName      having count(peopleName) > 1)
and   peopleId not in (select min(peopleId) from people group by peopleName     having count(peopleName)>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)   

6.消除一个字段的左边的第一位:

update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%'

7.消除一个字段的右边的第一位:

update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%村'

8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录
update vitae set ispass=-1
where peopleId in (select peopleId from vitae group by peopleId

 

时间: 2024-10-03 23:39:41

用SQL语句,删除掉重复项只保留一条的相关文章

SQL删除重复数据只保留一条

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

select-Mysql删除重复数据只保留一条

问题描述 Mysql删除重复数据只保留一条 Mysql删除重复数据只保留一条,我的sql这样的 DELETE from t_patient WHERE pa_iid IN (select pa_iid from t_patient group by pa_vname having count(pa_vname)>1) 但是报错了: [Err] 1093 - You can't specify target table 't_patient' for update in FROM clause 求

JS数组去掉重复数据只保留一条的实现代码_javascript技巧

非常不多说,js数组去掉重复数据的代码如下所示: var arr = [1,2,3,4,5,6,1,6,7,2]; var newArr = []; for(var i =0;i<arr.length-1;i++){ if(newArr.indexOf(arr[i]) == -1){ newArr.push(arr[i]); } } 下面再给大家分享高效率去掉js数组中重复项 Array类型并没有提供去重复的方法,如果要把数组的重复元素干掉,那得自己想办法: function unique(ar

oracle删除重复数据只保留一条

重复的数据可能有这样两种情况,第一种: 表中只有某些字段一样,第二种是两行记录完全一样. 一.对于部分字段重复数据的删除 1.查询重复的数据 select 字段1,字段2, count(*) from 表名 group by 字段1,字段2 having count(*) > 1 例:Select owner from dba_tables group by owner having count(*)>1; Select owner from dba_tables group by owner

SQL语句实现删除重复记录并只保留一条_数据库其它

复制代码 代码如下: delete WeiBoTopics where Id in(select max(Id) from WeiBoTopics group by WeiBoId,Title having COUNT(*) > 1); SQL:删除重复数据,只保留一条用SQL语句,删除掉重复项只保留一条在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 复制代码 代码如下:  select * f

SQL语句删除重复记录

问题:如何把具有相同字段的记录删除,只留下一条.   例如:表test里有id,name字段,如果有name相同的记录只留下一条,其余的删除.name的内容不定,相同的记录数不定.   用SQL语句删除重复记录的方法: 1.将重复的记录记入temp1表   select [标志字段id],count(*) into temp1 from [表名] group by [标志字段id] having count(*)>1 2.将不重复的记录记入temp1表   insert temp1 select

你真的会玩SQL吗?删除重复数据且只保留一条

在网上看过一些解决方法 我在此给出的方法适用于无唯一ID的情形 表:TB_MACVideoAndPicture 字段只有2个:mac,content mac作为ID,正常情况下mac数据是唯一的,由于操作失误导致数据插入多次,导致出现多个mac,content重复数据,现在只保留一条,删除多余的 大体思想是给重复数据一个自增ID,过滤出每组里面最小ID,删除原数据中所有重复数据再将最小ID插入 --查询出所有重复数据,并给定递增id SELECT IDENTITY( INT,1,1 ) AS i

c#-sql语句删除一条记录有问题,毕业设计帮帮忙,能帮帮我吗?

问题描述 sql语句删除一条记录有问题,毕业设计帮帮忙,能帮帮我吗? enter code here DB db = new DB(); string UserName = this.txtUserName.Text; string PassWord = db.GetMD5(this.txtPwd.Text.ToString());//MD5加密 string Code = this.txtCode.Text; string cmdstr = "insert into tb_User(UserN

JS实现合并两个数组并去除重复项只留一个的方法_javascript技巧

本文实例讲述了JS实现合并两个数组并去除重复项只留一个的方法.分享给大家供大家参考,具体如下: //It's merge arr1 and arr2 , delete the same element only leave one //It's only apdapter array. If object, no. //The sequence of the two array is not required. mergeArray:function (arr1, arr2){ for (var