zoukankan      html  css  js  c++  java
  • Distinct函数的替代方法

    Distinct函数的替代方法
     
     
        今天在论坛上看到一个面试题,是说有什么办法可以替代distinct,得到同样的结果。答案都被大家说的差不多了,发现挺有意思的,就记录一下:

    SQL> select num from t1;

           NUM
    ----------
             6
             6
             7
             8
             9
             1
             1
             1
           1
            1
             1
             1
            1
             1
            1

    15 rows selected
     
    SQL> select distinct num from t1;

           NUM
    ----------
             1
             6
             8
            7
            9

    5 rows selected



    一、用unique代替distinct:

    这个比较无耻,基本属于说了跟没说一样,但确实是对的

    SQL> select unique num from t1;

           NUM
    ----------
             1
             6
             8
            7
            9

    5 rows selected


    二、用group by来做:

    这个应该是出题者的本意

    SQL> select num from t1 group by num;

           NUM
    ----------
             1
             6
             8
             7
             9

    5 rows selected


    三、用union和minus:

    因为union和minus默认都是先distinct然后再做聚集,所以可以这样做:

    SQL> select num from t1 minus select 999 from dual;

           NUM
    ----------
             1
             6
             7
             8
             9

    5 rows selected
     
    SQL> select num from t1 union select num from t1 ;

           NUM
    ----------
             1
             6
             7
             8
             9

    5 rows selected

    一个是minus一个没有的项,一个是union它本身。



    这篇文章是从网上转载的,在实际项目中运到了MARK一下
     
  • 相关阅读:
    __declspec关键字详细用法
    【转载】前端面试“http全过程”将所有HTTP相关知识抛出来了...
    【转载】理解C语言中的关键字extern
    Linux应用环境实战
    【转载】深入解析连接点
    【转载】COM多线程原理与应用
    【转载】COM的多线程模型
    【转载】COM 连接点
    【转载】COM:IUnknown、IClassFactory、IDispatch
    101. Symmetric Tree
  • 原文地址:https://www.cnblogs.com/your568/p/2513535.html
Copyright © 2011-2022 走看看