删除重复记录的SQL语句
1.所有字段均重复的记录(重复记录保留一条)
Select distinct * into #Tmp from tblName
Drop table tblName
Select * into tblName from #Tmp
Drop table #Tmp
设计不周产生的,增加唯一索引可解决
2.所有字段均重复的记录(重复记录保留一条)
Select distinct * into #Tmp from tblName
Drop table tblName
Select * into tblName from #Tmp
Drop table #Tmp
设计不周产生的,增加唯一索引可解决
3.保留ID最小的记录,删除其它行
Delete from tblName where ID not in (select min(ID) from tblName group Name)
Delete from tblName t inner join (select min(ID) id,Name from tblName group by Name) b on t.name=b.name and t.id<>b.id
Delete from tblName where exists (select * from tblName where name=t.name and id<t.id)
4.只保留ID最大的记录
- Delete from tblName where ID not in (select max(ID) from tblName group by Name having count(*)>1)
- Delete from tblName t inner join (select Name,max(ID) id from tblName group by name) b on t.name=b.name and t.id<>b.id
- Delete from tblName t where exists (select * from tblName where name=t.name and id>t.id)
删除重复记录的SQL语句的更多相关文章
- [SQL]查询及删除重复记录的SQL语句
一:查询及删除重复记录的SQL语句1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select ...
- Oracle 查询并删除重复记录的SQL语句
查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select ...
- oracle 查询及删除重复记录的SQL语句
查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 where Id in (select Id from 表 group ...
- Sql server 删除重复记录的SQL语句
原文地址:https://www.cnblogs.com/luohoufu/archive/2008/06/05/1214286.html 在项目开发过程中有个模块日清表页面数据重复,当时那个页面的数 ...
- 查询及删除重复记录的SQL语句
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from ...
- 删除Mysql数据表中多余的重复记录的sql语句
数据表 sniper_tb 中存在主键 id,字段url,现需要在url字段上添加 unique,但由于url存在重复记录,导致添加失败. 如何删除表中多余的url重复记录,仅保持一条? 思路一 将 ...
- 从表中删除重复记录的sql
--有一个表,假设是这样的 CREATE TABLE Test ( field1 ) primary key, field2 )); --假设field1上有索引. 要删除表中所有field1重复的记 ...
- 常用判断重复记录的SQL语句
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from people where peopleId in (select peopleId fro ...
- SQL操作语句之查询及删除重复记录的方法
delete from 表 where id not in(select min(id) from 表 group by name ) //删除重复名字的记录 删除之前请用语句 select * fr ...
随机推荐
- Kernel-Scheduler implementation
2033 const struct sched_class rt_sched_class = { 2034 .next = &fair_sched_class, 2035 .enqueue_t ...
- COJ 3007 Mr.Yang的小助手
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1037 试题描述: 信息学社团已经逐渐发展壮大,成员也越来越多.现在,有n个 ...
- hdu-2586-How far away ?(离线LCA)
题意: 给定一棵树,每条边都有一定的权值,q次询问,每次询问某两点间的距离. 分析: 这样就可以用LCA来解,首先找到u, v 两点的lca,然后计算一下距离值就可以了. 这里的计算方法是,记下根结点 ...
- linkedin和facebook的区别
摘录一段百科(http://www.baike.com/wiki/LinkedIn)的文字: Linkedin - 特点 Linked是一个“高效”.“安全”并且“有商务价值”的“白领SNS提供商”: ...
- CSS3中text-overflow支持以...代替超出文本
CSS3中text-overflow支持以...代替超出文本. 1.div1:默认状态.超出文本默认显示在div外 2.div2:text-overflow:ellipsis; 使用text-over ...
- Happy Number——LeetCode
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- FZYZ-2071 A Simple Math Problem IX
P2071 -- A Simple Math Problem IX 时间限制:1000MS 内存限制:262144KB 状态:Accepted 标签: 数学问题-博弈论 ...
- 解决wordpress的fonts.googleapis.com在国内无法访问的问题
因为wordpress及主题的样式中几乎都使用了fonts.googleapis.com,而国内因为你知道的原因,是无法正常使用google服务的,因此导致在很多国内设备上打不开wordpress页面 ...
- 动态规划——F 最大矩阵和
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...
- ActionScript接收socket服务器发送来的数据
原文地址:http://www.asp119.com/news/2009522181815_1.htm 从socket中接收数据的方法取决于你使用socket类型,Socket和XMLSocket都可 ...