主页 > 知识库 > oracle中distinct的用法详解

oracle中distinct的用法详解

热门标签:哪个400外呼系统好 山东crm外呼系统软件 地图标注养老院 哈尔滨电话机器人销售招聘 慧营销crm外呼系统丹丹 百度地图标注途经点 图吧网站地图标注 开发外呼系统 爱客外呼系统怎么样

下面先来看看例子:

table表

字段1     字段2    id        name    1           a    2           b    3           c    4           c    5           b

库结构大概这样,这只是一个简单的例子,实际情况会复杂得多。

比如我想用一条语句查询得到name不重复的所有数据,那就必须使用distinct去掉多余的重复记录。

select distinct name from table 得到的结果是:

----------

name    a    b    c

好像达到效果了,可是,我想要得到的是id值呢?改一下查询语句吧:

select distinct name, id from table

结果会是:

----------

id name    1 a    2 b    3 c    4 c    5 b

distinct怎么没起作用?作用是起了的,不过他同时作用了两个字段,也就是必须得id与name都相同的才会被排除。。。。。。。

我们再改改查询语句:

 select id, distinct name from table

很遗憾,除了错误信息你什么也得不到,distinct必须放在开头。难到不能把distinct放到where条件里?能,照样报错。

------------------------------------------------------------------------------------------------------------

下面方法也不可行:

select *, count(distinct name) from table group by name

结果:

ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"

依然报错, 

group by 必须放在 order by 和 limit之前,不然会报错

------------------------------------------------------------------------------------------------------------

偶认为这样可行

select max(id), name from table group by name;

结果:

id name
1  a
2  b
4  c
5  d

用法二:

一、数据:

1    zjx    001    AAAiBZAAQAAAAVPAAA
2    zjx    002    AAAiBZAAQAAAAVPAAB
3    zjx    001    AAAiBZAAQAAAAVPAAC

二、多字段

select distinct t.name,t.code from test1 t
select distinct t.* from test1 t

结果:

1    zjx    001
2    zjx    002

三、单字段

select distinct t.name from test1 t

结果:

1    zjx

您可能感兴趣的文章:
  • 使用distinct在mysql中查询多条不重复记录值的解决办法
  • distinct 多列问题结合group by的解决方法
  • sqlserver中distinct的用法(不重复的记录)
  • 使用GROUP BY的时候如何统计记录条数 COUNT(*) DISTINCT
  • oracle sql 去重复记录不用distinct如何实现
  • 为何Linq的Distinct实在是不给力
  • 解析mysql中:单表distinct、多表group by查询去除重复记录
  • MongoDB教程之聚合(count、distinct和group)
  • mongodb中使用distinct去重的简单方法
  • SQL中distinct的用法(四种示例分析)
  • SQL select distinct的使用方法
  • 针对distinct疑问引发的一系列思考

标签:周口 开封 青岛 武汉 承德 甘肃 和田 固原

巨人网络通讯声明:本文标题《oracle中distinct的用法详解》,本文关键词  oracle,中,distinct,的,用法,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《oracle中distinct的用法详解》相关的同类信息!
  • 本页收集关于oracle中distinct的用法详解的相关信息资讯供网民参考!
  • 推荐文章