【SQL】SELECT語句中集合運算子 UNION/INTERSECT/MINUS
SELECT語句中集合運算子
UNION/INTERSECT/MINUS
UNION/INTERSECT/MINUS
使用UNION(ALL)、INTERSECT、MINUS可以對子查詢的結果集進行結合運算:
建立測試表
create table test1 as select * from emp where deptno in (10,20);
create table test2 as select * from emp where deptno in (20,30);
測試表test1:包含scott.emp表中10號和20號部門員工資訊
SCOTT@PROD>select * from test1 order by deptno;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT 17-NOV-81 5000 10
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
8 rows selected.
測試表test2:包含scott.emp表中20號和30號部門員工資訊
SCOTT@PROD>select * from test2 order by deptno;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
11 rows selected.
UNION:兩個子查詢並集,去重,排序
SCOTT@PROD>select empno,ename,job,deptno from test1
union select empno,ename,job,deptno from test2;
EMPNO ENAME JOB DEPTNO
---------- ---------- --------- ----------
7369 SMITH CLERK 20
7499 ALLEN SALESMAN 30
7521 WARD SALESMAN 30
7566 JONES MANAGER 20
7654 MARTIN SALESMAN 30
7698 BLAKE MANAGER 30
7782 CLARK MANAGER 10
7788 SCOTT ANALYST 20
7839 KING PRESIDENT 10
7844 TURNER SALESMAN 30
7876 ADAMS CLERK 20
7900 JAMES CLERK 30
7902 FORD ANALYST 20
7934 MILLER CLERK 10
14 rows selected.
UNION ALL:兩個子查詢並集,不去重,不排序
SCOTT@PROD>select empno,ename,job,deptno from test1
union all select empno,ename,job,deptno from test2;
EMPNO ENAME JOB DEPTNO
---------- ---------- --------- ----------
7369 SMITH CLERK 20
7566 JONES MANAGER 20
7782 CLARK MANAGER 10
7788 SCOTT ANALYST 20
7839 KING PRESIDENT 10
7876 ADAMS CLERK 20
7902 FORD ANALYST 20
7934 MILLER CLERK 10
7369 SMITH CLERK 20
7499 ALLEN SALESMAN 30
7521 WARD SALESMAN 30
7566 JONES MANAGER 20
7654 MARTIN SALESMAN 30
7698 BLAKE MANAGER 30
7788 SCOTT ANALYST 20
7844 TURNER SALESMAN 30
7876 ADAMS CLERK 20
7900 JAMES CLERK 30
7902 FORD ANALYST 20
19 rows selected.
INTERSECT:兩個子查詢交集,去重,排序
SCOTT@PROD>select empno,ename,job,deptno from test1
intersect select empno,ename,job,deptno from test2;
EMPNO ENAME JOB DEPTNO
---------- ---------- --------- ----------
7369 SMITH CLERK 20
7566 JONES MANAGER 20
7788 SCOTT ANALYST 20
7876 ADAMS CLERK 20
7902 FORD ANALYST 20
MINUS:兩個子查詢差集,去重,排序
SCOTT@PROD>select empno,ename,job,deptno from test1
minus select empno,ename,job,deptno from test2;
EMPNO ENAME JOB DEPTNO
---------- ---------- --------- ----------
7782 CLARK MANAGER 10
7839 KING PRESIDENT 10
7934 MILLER CLERK 10
呂星昊
2015.3.24
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29475508/viewspace-1472168/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle的集合操作(union、union all、intersect、minus集合函式)Oracle函式
- ORACLE中union/union all/Intersect/Minus用法Oracle
- Oracle中的Union、Union All、Intersect、MinusOracle
- sql_intersect交集_minus差集_並集union_union allSQL
- SQL Server中的集合運算: UNION, EXCEPT和INTERSECTSQLServer
- Oracle 中Union、Union All、Intersect、Minus(並,交,差)Oracle
- 【SQL 學習】INTERSECT,MINUS ,SQL
- 查詢集合操作intersect與minus
- sql語句中select……as的用法SQL
- SQL語句中SELECT語句的執行順序SQL
- 【SQL查詢】集合查詢之INTERSECTSQL
- OCP課程9:SQL之使用集合運算子SQL
- MySQL實現差集(Minus)和交集(Intersect)MySql
- SQL語言運算子與函式SQL函式
- SQL語句運算子最佳化SQL
- 【學習】SQL基礎-008-集合運算子SQL
- 【SQL】14 UNION 操作符、SELECT INTO 語句、INSERT INTO SELECT 語句、CREATE DATABASE 語句、CREATE TABLE 語句SQLDatabase
- sql語句中as的用法SQL
- PL/SQL 運算子SQL
- WPF Path GeometryCombineMode Union, Exclude,Intersect,Xor
- sql語句中JOIN ON 的使用SQL
- Oracle OCP(09):使用集合運算子Oracle
- sql語句中#{}和${}的區別SQL
- sql語句中as的用法和作用SQL
- Go語言運算子Go
- Go 語言運算子Go
- C語言運算子C語言
- 查詢集合操作union與union all
- ORACLE中sql語句----運算子的優先順序OracleSQL
- SQL語句中exists和in的區別SQL
- SQL語句中not in 和not exist的區別SQL
- sql語句中as的意思是什麼SQL
- 在sql語句中替換Not In 的方法SQL
- sql語句中常量的處理SQL
- SQL SELECT 語句SQL
- Oracle SQL語句優化之UNIONOracleSQL優化
- SQL Union和SQL Union All用法(轉)SQL
- java基本語法--運算子Java