美文网首页
集合运算

集合运算

作者: 传棋Jaking | 来源:发表于2018-06-12 16:10 被阅读0次

    1.union

    SQL> conn scott/oracle 
    Connected.
    SQL> select * from emp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7369 SMITH      CLERK       7902 17-DEC-80        800            20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300     30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500     30
          7566 JONES      MANAGER         7839 02-APR-81       2975            20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400     30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850            30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000            20
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500      0     30
          7876 ADAMS      CLERK       7788 23-MAY-87       1100            20
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7900 JAMES      CLERK       7698 03-DEC-81        950            30
          7902 FORD       ANALYST         7566 03-DEC-81       3000            20
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
    
    14 rows selected.
    
    SQL> create table temp as select * from emp where deptno=10;
    
    Table created.
    
    SQL> select * from temp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
    
    SQL> select * from temp 
      2  union
      3  select * from emp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7369 SMITH      CLERK       7902 17-DEC-80        800            20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300     30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500     30
          7566 JONES      MANAGER         7839 02-APR-81       2975            20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400     30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850            30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000            20
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500      0     30
          7876 ADAMS      CLERK       7788 23-MAY-87       1100            20
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7900 JAMES      CLERK       7698 03-DEC-81        950            30
          7902 FORD       ANALYST         7566 03-DEC-81       3000            20
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
    
    14 rows selected.
    
    SQL> insert into temp values (7935,'ALVIN','CLERK',7698,sysdate,1300,null,30);
    
    1 row created.
    
    SQL> select * from temp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
          7935 ALVIN      CLERK       7698 12-JUN-18       1300            30
    
    SQL> commit;
    
    Commit complete.
    
    SQL> select * from emp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7369 SMITH      CLERK       7902 17-DEC-80        800            20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300     30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500     30
          7566 JONES      MANAGER         7839 02-APR-81       2975            20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400     30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850            30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000            20
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500      0     30
          7876 ADAMS      CLERK       7788 23-MAY-87       1100            20
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7900 JAMES      CLERK       7698 03-DEC-81        950            30
          7902 FORD       ANALYST         7566 03-DEC-81       3000            20
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
    
    14 rows selected.
    
    SQL> select * from temp 
      2  union
      3  select * from emp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7369 SMITH      CLERK       7902 17-DEC-80        800            20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300     30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500     30
          7566 JONES      MANAGER         7839 02-APR-81       2975            20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400     30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850            30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000            20
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500      0     30
          7876 ADAMS      CLERK       7788 23-MAY-87       1100            20
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7900 JAMES      CLERK       7698 03-DEC-81        950            30
          7902 FORD       ANALYST         7566 03-DEC-81       3000            20
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
          7935 ALVIN      CLERK       7698 12-JUN-18       1300            30
    
    15 rows selected.
    
    SQL> insert into temp values (7935,'ALVIN','CLERK',7698,sysdate,1300,null,40);
    
    1 row created.
    
    SQL> select * from temp
      2  union
      3  select * from emp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7369 SMITH      CLERK       7902 17-DEC-80        800            20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300     30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500     30
          7566 JONES      MANAGER         7839 02-APR-81       2975            20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400     30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850            30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000            20
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500      0     30
          7876 ADAMS      CLERK       7788 23-MAY-87       1100            20
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7900 JAMES      CLERK       7698 03-DEC-81        950            30
          7902 FORD       ANALYST         7566 03-DEC-81       3000            20
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
          7935 ALVIN      CLERK       7698 12-JUN-18       1300            30
          7935 ALVIN      CLERK       7698 12-JUN-18       1300            40
    
    16 rows selected.
    
    SQL> 
    
    

    联合查询内容不对应的表

    SQL> select * from temp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
          7935 ALVIN      CLERK       7698 12-JUN-18       1300            30
          7935 ALVIN      CLERK       7698 12-JUN-18       1300            40
    
    SQL> select * from dept;
    
        DEPTNO DNAME      LOC
    ---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH   DALLAS
        30 SALES      CHICAGO
        40 OPERATIONS     BOSTON
    
    SQL> select * from temp
      2  union all
      3  select * from dept;
    select * from temp
    *
    ERROR at line 1:
    ORA-01789: query block has incorrect number of result columns
    
    
    SQL> select * from temp
      2  union
      3  select * from dept;
    select * from temp
    *
    ERROR at line 1:
    ORA-01789: query block has incorrect number of result columns
    #以上格式不对,均会报错。
    
    SQL> select * from temp 
      2  union all
      3  select dept.*,null,null,null,null,null from dept;
    
         EMPNO ENAME      JOB              MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- -------------- ------------- ---------- --------- ---------- ---------- ----------
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7934 MILLER     CLERK           7782 23-JAN-82       1300            10
          7935 ALVIN      CLERK           7698 12-JUN-18       1300            30
          7935 ALVIN      CLERK           7698 12-JUN-18       1300            40
        10 ACCOUNTING     NEW YORK
        20 RESEARCH   DALLAS
        30 SALES      CHICAGO
        40 OPERATIONS     BOSTON
    
    9 rows selected.
    
    SQL> select * from temp
      2  union
      3  select dept.*,null,null,null,null,null from dept;
    
         EMPNO ENAME      JOB              MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- -------------- ------------- ---------- --------- ---------- ---------- ----------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH   DALLAS
        30 SALES      CHICAGO
        40 OPERATIONS     BOSTON
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7934 MILLER     CLERK           7782 23-JAN-82       1300            10
          7935 ALVIN      CLERK           7698 12-JUN-18       1300            30
          7935 ALVIN      CLERK           7698 12-JUN-18       1300            40
    
    9 rows selected.
    
    SQL> select * from temp
      2  union all
      3  select dept.*,null,null,null,null from dept;
    select * from temp
    *
    ERROR at line 1:
    ORA-01789: query block has incorrect number of result columns
    #格式不对,少了一个null,空值需要用null来代替。
    
    SQL> 
    
    

    2.intersect

    SQL> select * from temp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
          7935 ALVIN      CLERK       7698 12-JUN-18       1300            30
          7935 ALVIN      CLERK       7698 12-JUN-18       1300            40
    
    SQL> select * from emp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7369 SMITH      CLERK       7902 17-DEC-80        800            20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300     30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500     30
          7566 JONES      MANAGER         7839 02-APR-81       2975            20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400     30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850            30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000            20
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500      0     30
          7876 ADAMS      CLERK       7788 23-MAY-87       1100            20
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7900 JAMES      CLERK       7698 03-DEC-81        950            30
          7902 FORD       ANALYST         7566 03-DEC-81       3000            20
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
    
    14 rows selected.
    
    SQL> select * from temp
      2  intersect
      3  select * from emp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
    
    SQL> 
    
    
    SQL> select * from emp 
      2  intersect 
      3  select * from emp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7369 SMITH      CLERK       7902 17-DEC-80        800            20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300     30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500     30
          7566 JONES      MANAGER         7839 02-APR-81       2975            20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400     30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850            30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000            20
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500      0     30
          7876 ADAMS      CLERK       7788 23-MAY-87       1100            20
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7900 JAMES      CLERK       7698 03-DEC-81        950            30
          7902 FORD       ANALYST         7566 03-DEC-81       3000            20
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
    
    14 rows selected.
    
    

    3.minus

    SQL> select * from temp
      2  minus
      3  select * from emp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7935 ALVIN      CLERK       7698 12-JUN-18       1300            30
          7935 ALVIN      CLERK       7698 12-JUN-18       1300            40
    
    SQL> select * from emp
      2  minus
      3  select * from temp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7369 SMITH      CLERK       7902 17-DEC-80        800            20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300     30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500     30
          7566 JONES      MANAGER         7839 02-APR-81       2975            20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400     30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850            30
          7788 SCOTT      ANALYST         7566 19-APR-87       3000            20
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500      0     30
          7876 ADAMS      CLERK       7788 23-MAY-87       1100            20
          7900 JAMES      CLERK       7698 03-DEC-81        950            30
          7902 FORD       ANALYST         7566 03-DEC-81       3000            20
    
    11 rows selected.
    
    
    SQL> select * from emp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7369 SMITH      CLERK       7902 17-DEC-80        800            20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300     30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500     30
          7566 JONES      MANAGER         7839 02-APR-81       2975            20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400     30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850            30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000            20
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500      0     30
          7876 ADAMS      CLERK       7788 23-MAY-87       1100            20
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7900 JAMES      CLERK       7698 03-DEC-81        950            30
          7902 FORD       ANALYST         7566 03-DEC-81       3000            20
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
    
    14 rows selected.
    
    SQL> select * from temp;
    
         EMPNO ENAME      JOB          MGR HIREDATE     SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
          7782 CLARK      MANAGER         7839 09-JUN-81       2450            10
          7839 KING       PRESIDENT        17-NOV-81       5000            10
          7934 MILLER     CLERK       7782 23-JAN-82       1300            10
          7935 ALVIN      CLERK       7698 12-JUN-18       1300            30
          7935 ALVIN      CLERK       7698 12-JUN-18       1300            40
    
    
    

    相关文章

      网友评论

          本文标题:集合运算

          本文链接:https://www.haomeiwen.com/subject/xscreftx.html