8、set操作

八、set操作


set操作:类型与向导 types and guidelines

set操作符:set operators

union / union all     并集,去重复  /  不去重复
intersect    交集
minus   差集

set 操作向导:set operator guidelines
1、表达式必须数量上匹配
2、后一个语句列的类型必须与前一个数据类型一致
3、括号可以改变优先级
4、order by 出现在最后,但是是对第一个语句进行排序,与第二个无关
5、如果后一个语句没有前语句的列,可以使用相同类型的代替(数字或者字符)

例如:union和union all

SQL> select deptno,loc from dept
  2  union
  3  select deptno,loc from dept;

    DEPTNO LOC
---------- -------------
    10 NEW YORK
    20 DALLAS
    30 CHICAGO
    40 BOSTON

SQL> select deptno,loc from dept
  2  union all
  3  select deptno,loc from dept;

    DEPTNO LOC
---------- -------------
    10 NEW YORK
    20 DALLAS
    30 CHICAGO
    40 BOSTON
    10 NEW YORK
    20 DALLAS
    30 CHICAGO
    40 BOSTON

intersect

SQL> select deptno,loc,1 from dept
  2  intersect
  3  select deptno,loc,2 from dept;

no rows selected

SQL> select deptno,loc from dept
  2  intersect
  3  select deptno,loc from dept;

    DEPTNO LOC
---------- -------------
    10 NEW YORK
    20 DALLAS
    30 CHICAGO
    40 BOSTON

order by 语句
1、order by 只能出现一次,且只能在最后
2、组成的查询语句不能有order by 字句
3、只识别首查询列
4、默认首查询的第一列升序排序、降序desc


 

猜你喜欢

转载自blog.csdn.net/weixin_43403578/article/details/89281703