数据库去重
2016-04-13 21:49
344 查看
有重复数据主要有一下几种情况:
1.存在两条完全相同的纪录
这是最简单的一种情况,用关键字distinct就可以去掉
example: select distinct * from table(表名) where (条件)
2.存在部分字段相同的纪录(有主键id即唯一键)
如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组
example:
select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])
3.没有唯一键ID
这种情况我觉得最复杂,目前我只会一种方法,有那位知道其他方法的可以留言,交流一下:
example:
select identity(int1,1) as id,* into newtable(临时表) from table
select * from newtable where id in (select max(id) from newtable group by [去除重复的字段名列表,....])
drop table newtable
1.存在两条完全相同的纪录
这是最简单的一种情况,用关键字distinct就可以去掉
example: select distinct * from table(表名) where (条件)
2.存在部分字段相同的纪录(有主键id即唯一键)
如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组
example:
select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])
3.没有唯一键ID
这种情况我觉得最复杂,目前我只会一种方法,有那位知道其他方法的可以留言,交流一下:
example:
select identity(int1,1) as id,* into newtable(临时表) from table
select * from newtable where id in (select max(id) from newtable group by [去除重复的字段名列表,....])
drop table newtable
相关文章推荐
- Access转成Sql 2008步骤,同时解决自动编号问题,主键,id数值不重置
- oracle SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解
- 数据库事务的隔离级别
- MySQL优化必须调整的10项配置
- Java 使用 Redis
- Jena学习笔记(2)——利用数据库保存本体
- sqlserver中的dbo当用and 1=0/user SQL注入时
- Redis 数据类型
- Oracle 11g必须开启的服务及服务详细介绍
- Redis+linux的Bundle池化(四)
- 一个数据库异常的扩展函数帮助类
- 【MongoDB】spring-data-mongo配置
- Redis安装_Window+Linux
- Redis+linux自定义池化(三)
- mysqli的认识与应用
- 为什么数据库连接池异步的少
- MySQL基础六:运算符和函数
- 【Oracle】5.事务
- NewSQL体系比Hadoop更具效率
- SQL SERVER的字段类型说明