zoukankan      html  css  js  c++  java
  • Oracle minus用法详解及应用实例

    本文转载:https://blog.csdn.net/jhon_03/article/details/78321937

    Oracle minus用法

           “minus”直接翻译为中文是“减”的意思,在Oracle中也是用来做减法操作的,只不过它不是传统意义上对数字的减法,而是对查询结果集的减法。A minus B就意味着将结果集A去除结果集B中所包含的所有记录后的结果,即在A中存在,而在B中不存在的记录。其算法跟Java中的Collection的removeAll()类似,即A minus B将只去除A跟B的交集部分,对于B中存在而A中不存在的记录不会做任何操作,也不会抛出异常。

           Oracle的minus是按列进行比较的,所以A能够minus B的前提条件是结果集A和结果集B需要有相同的列数,且相同列索引的列具有相同的数据类型。此外,Oracle会对minus后的结果集进行去重,即如果A中原本多条相同的记录数在进行A minus B后将会只剩一条对应的记录,具体情况请看下面的示例。

            下面我们来看一个minus实际应用的示例,假设我们有一张用户表t_user,其中有如下记录数据:

    id

    no

    name

    age

    level_no

    1

    00001

    a

    25

    1

    2

    00002

    b

    30

    2

    3

    00003

    c

    35

    3

    4

    00004

    d

    45

    1

    5

    00005

    e

    30

    2

    6

    00006

    f

    35

    3

    7

    00007

    g

    25

    1

    8

    00008

    h

    35

    2

    9

    00009

    i

    20

    3

    10

    00010

    j

    25

    1

     那么:

           (1)“select id from t_user where id<6 minus select id from t_user where id between 3 and 7”的结果将为:

    id

    1

    2

           (2)“select age,level_no from t_user where id<8 minus select age,level_no from t_user where level=3”的结果为:

    age

    level_no

    25

    1

    30

    2

    45

    1

            看到这样的结果,可能你会觉得有点奇怪,为何会是这样呢?我们来分析一下。首先,“select age,level_no from t_user where id<8”的结果将是这样的:

    age

    level_no

    25

    1

    30

    2

    35

    3

    45

    1

    30

    2

    35

    3

    25

    1

           然后,“select age,level_no from t_user where level=3”的结果将是这样的:

    age

    level_no

    35

    3

    35

    3

    20

    3

           然后,直接A minus B之后结果应当是:

    age

    level_no

    25

    1

    30

    2

    45

    1

    30

    2

    25

    1

           这个时候,我们可以看到结果集中存在重复的记录,进行去重后就得到了上述的实际结果。其实这也很好理解,因为minus的作用就是找出在A中存在,而在B中不存在的记录。

    上述示例都是针对于单表的,很显然,使用minus进行单表操作是不具备优势的,其通常用于找出A表中的某些字段在B表中不存在对应记录的情况。比如我们拥有另外一个表t_user2,其拥有和t_user表一样的表结构,那么如下语句可以找出除id外,在t_user表中存在,而在t_user2表中不存在的记录。

    select no,name,age,level_no from t_user minus select no,name,age,level_no from t_user2;

  • 相关阅读:
    jquery.validate使用攻略 5
    jquery.validate使用攻略 4
    ccnet1.5集成tfs中文版的问题
    解决mysql连接异常—com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception
    Myeclipse 6.5 增加对 JavaEE 6 的支持
    Python3.7环境配置
    myeclipse 8.6 software and workspace center is currently not available
    入坑cordova
    开启博客之路
    如何在高并发的分布式系统中产生UUID
  • 原文地址:https://www.cnblogs.com/cq-yangzhou/p/10112372.html
Copyright © 2011-2022 走看看