distinct去除重复值
来源:互联网 发布:清华五道口考研知乎 编辑:程序博客网 时间:2024/05/17 22:15
在表中,可能会包含重复值。这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值。关键词 distinct用于返回唯一不同的值。
表A:
表B:
1.作用于单列
select distinct name from A
执行后结果如下:
2.作用于多列
示例2.1
select distinct name, id from A
执行后结果如下:
实际上是根据name和id两个字段来去重的,这种方式Access和SQL Server同时支持。
示例2.2
select distinct xing, ming from B
返回如下结果:
返回的结果为两行,这说明distinct并非是对xing和ming两列“字符串拼接”后再去重的,而是分别作用于了xing和ming列。
3.COUNT统计
select count(distinct name) from A; --表中name去重后的数目, SQL Server支持,而Access不支持
count是不能统计多个字段的,下面的SQL在SQL Server和Access中都无法运行。
select count(distinct name, id) from A;
若想使用,请使用嵌套查询,如下:
select count(*) from (select distinct xing, name from B) AS M;
4.distinct必须放在开头
select id, distinct name from A; --会提示错误,因为distinct必须放在开头
5.其他
distinct语句中select显示的字段只能是distinct指定的字段,其他字段是不可能出现的。例如,假如表A有“备注”列,如果想获取distinc name,以及对应的“备注”字段,想直接通过distinct是不可能实现的。但可以通过其他方法实现关于SQL Server将一列的多行内容拼接成一行的问题讨论
0 0
- distinct去除重复值
- DISTINCT,DISTINCTROW去除重复值
- mysql中distinct去除重复值方法
- SQL:DISTINCT 去除重复
- SQL:DISTINCT 去除重复
- Mysql| distinct去除重复的结果(DISTINCT)
- MySQL去除查询结果重复值 distinct 的用法
- MySQL去除查询结果重复值 distinct 的用法
- MySQL去除查询结果重复值 distinct 的用法
- SELECT DISTINCT去除SQL中的重复记录
- SQL之去除重复列(distinct )
- LINQ DISTINCT有效去除重复数据
- linq 中Distinct去除重复数据
- Linq List<T>.Distinct() 去除对象重复
- mysql中去除重复字段-distinct
- 返回字符串中的所有单词 $distinct=true 去除重复
- mysql 去除重复 Select中DISTINCT关键字的用法
- mysql 去除重复 Select中DISTINCT关键字的用法
- JS定时器来间断性的执行函数
- 选择排序
- Java基础学习应用_数组常用方法与排序(一)
- 碎片
- Linux下C语言汉字逆置
- distinct去除重复值
- 介绍5款非常棒的移动自动化测试工具
- Dialog 各种风格使用
- Networkx_找出最大联通子图及联通子图规模排序
- Java使用getResource
- library - 程序库
- HTML+JS 代码块加载顺序
- 指针赋值的一些说明
- lsb - 最低有效位