ITPub博客

首页 > 数据库 > Oracle > 查询中的distinct与group by

查询中的distinct与group by

原创 Oracle 作者:skyin_1603 时间:2016-10-09 15:57:25 0 删除 编辑

Distinct 与 group by :

本身不想拿distinct这个关键字来讲的,可是想着它在平时的查询中也经常会使用到。它只是指定去重复的一个关键字,
既不是函数,又不像
from与where这样决定查询语句结构的关键字。使用功能方面,有时候就只是去重功能,有时候起
到
group by 的功能。所以将它们两个专门拿到这里再讲一下。


1、背景数据: 

SQL> select mgr,deptno

  2  from emp;

       MGR     DEPTNO

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

      7902         20

      7698         30

      7698         30

      7839         20

      7698         30

      7839         30

      7839         10

      7566         20

                   10

      7698         30

      7788         20

       MGR     DEPTNO

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

      7698         30

      7566         20

      7782         10

14 rows selected.

2、distinct指定一个字段:

SQL> select distinct mgr

  2  from emp;

       MGR

----------

      7839


      7782

      7698

      7902

      7566

      7788

7 rows selected.


3、distinct
指定两个字段:

SQL> select distinct mgr,deptno

  2  from emp;

       MGR     DEPTNO

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

      7902         20

      7782         10

      7698         30

      7839         30

      7839         10

      7566         20

      7788         20

      7839         20

                   10

9 rows selected.

 相当是两个字段组合去重。


4、当查询有组类的单个字段:

SQL> select distinct deptno

  2  from emp;

    DEPTNO

----------

        30

        20

        10


5、查询中使用group by:

SQL>  select deptno

  2  from emp

  3  group by deptno;

    DEPTNO

----------

        30

        20

        10

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31392094/viewspace-2125999/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2016-09-06

  • 博文量
    231
  • 访问量
    215114