MSSQL 删除重复并只保留一条

重复值查找

select field1,field2 from table
group by field1,field2  having COUNT(*)>1

删除重复值,只保留ID最小的那条


delete table
where field1 in (select field1 from VersionProductRelation a
group by field1,field2 having COUNT(*)>1)

and field2 in (select field2 from VersionProductRelation a
group by field1,field2 having COUNT(*)>1)

and ID not in (
select MIN(ID) from table
group by field1,field2 having COUNT(*)>1)

原文地址:https://www.cnblogs.com/Alex-Mercer/p/12263095.html

时间: 2024-10-11 04:07:02

MSSQL 删除重复并只保留一条的相关文章

【转】SQL删除重复记录,只保留其中一条

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

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

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

MySQL中删除重复数据只保留一条

用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 SELECT * FROM people WHERE peopleId IN ( SELECT peopleId FROM people GROUP BY peopleId HAVINGcount(peopleId) > 1 ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3

Oralce中SQL删除重复数据只保留一条(转)

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

MySQL删除重复记录只保留一条

删除表中重复记录,只保留一条: delete from 表名 where 字段ID in (select * from (select max(字段ID) from 表名 group by 重复的字段 having count(重复的字段) > 1) as b); 实例:2.当想要为某一个表建立一个唯一索引,由于表中有重复记录而无法进行时,需要删除重复记录.例表 dept id_no id_name 100 'AAA' 101 'BBB' 102 'CCC' 103 'DDD' 100 'EEE

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

面试碰到一个MySQl的有趣的题目,如何从student表中删除重复名字的行,并保留最小id的记录? 很遗憾当时没有做出来,回家搜索了一番,发现利用子查询的可以很快解决. 1.删除表中多余的重复记录,重复记录是username判断,只留有id最小的记录 delete from studentwhere username in ( select username from studentgroup by username having count(username)>1) and id not i

SqlServer_删除重复行只保留一条记录

前提:相同的数据重复往数据库写入,导致存在仅主键Id不同的重复数据,现在需要去除重复数据,仅保留重复数据中Id最大的一条 思路: 1.找出存在重复数据的记录,并取重复数据中最大的Id值 2.删除记录中不包含最大Id值的记录 实现: DELETE FROM [Log_IIS].[dbo].[IISLog_table] WHERE id not in (select MAX(Id) from [IISLog_table] group by [RequestTime] ,[LogFilename] ,

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

1.表结构与数据: CREATE TABLE tablezzl( id int, name VARCHAR(255) ); 2.查询出重复的数据: SELECT name FROM tablezzl GROUP BY name HAVING COUNT(name)>1 3.查询出要保留的重复数据: SELECT MIN(id) ids FROM tablezzl GROUP BY NAME HAVING COUNT(name)>1 4.最终的SQL: DELETE FROM tablezzl

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

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