SQL查询语句去除重复行
2016-03-11 16:44
357 查看
[b]1.存在两条完全相同的纪录[/b]
这是最简单的一种情况,用关键字distinct就可以去掉
select distinct * from table(表名) where (条件)
[b]2.存在部分字段相同的纪录(有主键id即唯一键)[/b]
如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组
select * from table where id in (select min(id) from table group by [去除重复的字段名列表,....])
[取重复的第一行]
或者
select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])
[取重复的最后一行]
[b]3.没有唯一键ID[/b]
select identity(int,1,1) as id,* into newtable(临时表) from table
select * from newtable where id in (select min(id) from newtable group by [去除重复的字段名列表,....])
drop table newtable
扩展:
1、identity(int,1,1) ——从1开始,每次递增1
2、SELECT INTO 语句 —— SELECT * INTO new_table_name [IN externaldatabase] FROM old_tablename
------------------------------------------------------------------------------------------------------
引自:
sql查询语句去除重复列(行)【http://blog.knowsky.com/234240.htm】
这是最简单的一种情况,用关键字distinct就可以去掉
select distinct * from table(表名) where (条件)
[b]2.存在部分字段相同的纪录(有主键id即唯一键)[/b]
如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组
select * from table where id in (select min(id) from table group by [去除重复的字段名列表,....])
[取重复的第一行]
或者
select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])
[取重复的最后一行]
[b]3.没有唯一键ID[/b]
select identity(int,1,1) as id,* into newtable(临时表) from table
select * from newtable where id in (select min(id) from newtable group by [去除重复的字段名列表,....])
drop table newtable
扩展:
1、identity(int,1,1) ——从1开始,每次递增1
2、SELECT INTO 语句 —— SELECT * INTO new_table_name [IN externaldatabase] FROM old_tablename
------------------------------------------------------------------------------------------------------
引自:
sql查询语句去除重复列(行)【http://blog.knowsky.com/234240.htm】
相关文章推荐
- 如何在windows中安装Oracle的SQLPlus
- 一次Redis TTL 为0的问题排查
- MongoDB Java 学习笔记
- sqlserver数据库还原语句
- PowerDesigner之CDM、PDM、SQL之间转换
- mongodb 备份和还原
- mysql5.5 一键重置mysql密码
- 【胖鱼头】基于Sentinel的Redis主从分片集群搭建
- vc连接access数据库及数据表基本操作
- jdbc 打印执行sql日志包括参数
- mysql 常用工具 用户 权限
- 使用pl/sql备份数据
- windows+mysql集群搭建-三分钟搞定集群
- Oracle函数to_char应用示例
- Oracle11g创建表空间语句
- MYSQL之You can't specify target table for update in FROM clause解决办法
- 数据库索引
- hive 操作(二)——使用 mysql 作为 hive 的metastore
- 读取redis中的数据时出现:MISCONF Redis is configured to save RDB snapshots
- oracle创建database links语句