zoukankan      html  css  js  c++  java
  • orcale 之 集合操作

      集合操作就是将两个或者多个 sql 查询的结果合并成复合查询。常见的集合操作有UNION(并运算)、UNION ALL、INTERSECT(交运算)和MINUS(差运算)。

    UNION

      UNION 运算可以将多个查询结果集相加,形成一个结果集, 其结果相当于集合运算的并运算. UNION 可以将第一个查询结果的所有行与第二个结果集的所有行进行相加,行切消除所有的重复行.语法如下:

    SELECT column1 [, column2 ]
    FROM table1 [, table2 ]
    [WHERE condition]
    
    UNION
    
    SELECT column1 [, column2 ]
    FROM table1 [, table2 ]
    [WHERE condition]

    例子: 查询名字以 S 或者 M 开始的员工所有信息

     select *
     from emp
     where ename like 'S%'
     UNION
     select *
     from emp
     where ename like 'M%';

    UNION ALL

      UNION ALL 的作用和 UNION 的作用极为相似不同之处就是形成的结果集包含其重复的部分。

    SELECT column1 [, column2 ]
    FROM table1 [, table2 ]
    [WHERE condition]
    
    UNION ALL
    
    SELECT column1 [, column2 ]
    FROM table1 [, table2 ]
    [WHERE condition]

    例子: 查询名字以 S 结尾或者 M 开始的员工所有信息

     select *
     from emp
     where ename like '%R'
     UNION ALL
     select *
     from emp
     where ename like 'M%';

    INTERSECT

      INTERSECT 也用于对两个集合进行操作,与之不同的是用于取交集预算。语法如下:

    SELECT column1 [, column2 ]
    FROM table1 [, table2 ]
    [WHERE condition]
    
    INTERSECT
    
    SELECT column1 [, column2 ]
    FROM table1 [, table2 ]
    [WHERE condition]

    例子: 查询名字以 R 结尾并且 M 开始的员工所有信息

    select *
    from emp
    where ename like '%R'
    INTERSECT
    select *
    from emp
    where ename like 'M%';

    MINUS

      MINUS 用于查询两个集合的差集,也就是说该运算用于返回所有从第一个查询中返回,但是没有在第二个查询中返回的记录。语法如下:

    SELECT column1 [, column2 ]
    FROM table1 [, table2 ]
    [WHERE condition]
    
    MINUS
    
    SELECT column1 [, column2 ]
    FROM table1 [, table2 ]
    [WHERE condition]

    例子:查询名字以 R 结尾但是不以 M 开始的员工所有信息

    select *
    from emp
    where ename like '%R'
    MINUS
    select *
    from emp
    where ename like 'M%';
  • 相关阅读:
    JavaScript模态对话框类
    事件模块的演变(1)
    html5中可通过document.head获取head元素
    How to search for just a specific file type in Visual Studio code?
    What do 'lazy' and 'greedy' mean in the context of regular expressions?
    正则非获取匹配 Lookahead and Lookbehind ZeroLength Assertions
    regex length 正则长度问题
    Inversion of Control vs Dependency Injection
    How to return View with QueryString in ASP.NET MVC 2?
    今天才发现Google Reader
  • 原文地址:https://www.cnblogs.com/brother-four/p/6426362.html
Copyright © 2011-2022 走看看