1.֪ʶµã£º¿ÉÒÔ¶ÔÕÕÏÂÃæµÄ¼ÆÁ½øÐÐÔĶÁ
SQL> -- ²éѯ10ºÍ20ºÅ²¿ÃŵÄÔ±¹¤µÄ3ÖÖ·½·¨
SQL> --1. select * from emp where deptno in (10,20);
SQL> --2. select * from emp where deptno=10 or deptno=20;
SQL> --3. ¼¯ºÏÔËË㣺union/union all²¢¼¯£»intersect ½»¼¯£»minus²î¼¯
SQL> -- select * from emp where deptno=10
SQL> -- +
SQL> -- select * from emp where deptno=20;
SQL> --union£º²¢¼¯£¬Öظ´µÄÇøÓòֻȡһ´Î£»union all£º²¢¼¯£¬Öظ´µÄÇøÓòÈ¡Á½´Î
SQL> select * from emp where deptno=10
2 union
3 select * from emp where deptno=20;
SQL> --ÀûÓü¯ºÏÔËËãʵÏÖgroup byµÄÔöÇ¿
SQL> --×¢Òâ
SQL> --1. ²ÎÓëÔËËãµÄ¸÷¸ö¼¯ºÏ±ØÐëÁÐÊýÏàͬ ÇÒÀàÐÍÒ»ÖÂ
SQL> --2. ²ÉÓõÚÒ»¸ö¼¯ºÏµÄ±íÍ·×÷Ϊ×îºóµÄ±íÍ·
SQL> --3. Èç¹ûÅÅÐò£¬±ØÐëÔÚÿ¸ö¼¯ºÏºóʹÓÃÏàͬµÄorder by
SQL> --4. ¿ÉÒÔʹÓÃÀ¨ºÅ
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
SQL> --´ò¿ªsqlÖ´ÐÐʱ¼ä£¬Ã¿´ÎÖ´ÐÐsqlÓï¾ä¶¼»áÏÔʾִÐÐʱ¼ä
SQL> set timing on
SQL> --¹Ø±ÕsqlÖ´ÐÐʱ¼ä
SQL> set timing off
SQL> --½»¼¯£º¼ÈÊôÓÚintersectÇ°ÃæµÄ¼¯ºÏÓÖÊôÓÚintersectºóÃæµÄ¼¯ºÏ
SQL> select ename,sal from emp
2 where sal between 700 and 1300
3 INTERSECT
4 select ename,sal from emp
5 where sal between 1201 and 1400;
SQL> --²î¼¯£ºÖ»ÊôÓÚminusÇ°ÃæµÄ¼¯ºÏ¶ø²»ÊôÓÚminusºóÃæµÄ¼¯ºÏ
SQL> select ename,sal from emp
2 where sal between 700 and 1300
3 minus
4 select ename,sal from emp
5 where sal between 1201 and 1400;
2.ÔÚSqlplusÏÂʵ¼ÊÖ´ÐеĽá¹û¼ÆÁ
SQL> /*
SQL> ²éѯ10ºÍ20ºÅ²¿ÃŵÄÔ±¹¤
SQL> 1. select * from emp where deptno in (10,20);
SQL> 2. select * from emp where deptno=10 or deptno=20;
SQL> 3. ¼¯ºÏÔËËã
SQL> select * from emp where deptno=10
SQL> +
SQL> select * from emp where deptno=20;
SQL> */
SQL> select * from emp where deptno=10
2 union
3 select * from emp where deptno=20;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ------
7369 SMITH CLERK 7902 17-12ÔÂ-80 800 20
7566 JONES MANAGER 7839 02-4ÔÂ -81 2975 20
7782 CLARK MANAGER 7839 09-6ÔÂ -81 2450 10
7788 SCOTT ANALYST 7566 13-7ÔÂ -87 3000 20
7839 KING PRESIDENT 17-11ÔÂ-81 5000 10
7876 ADAMS CLERK 7788 13-7ÔÂ -87 1100 20
7902 FORD ANALYST 7566 03-12ÔÂ-81 3000 20
7934 MILLER CLERK 7782 23-1ÔÂ -82 1300 10
ÒÑÑ¡Ôñ8ÐС£
SQL> --ÀûÓü¯ºÏÔËËãʵÏÖgroup byµÄÔöÇ¿
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,sum(sal) from emp group by deptno
4 union
5 select sum(sal) from emp;
select deptno,sum(sal) from emp group by deptno
*
µÚ 3 ÐгöÏÖ´íÎó:
ORA-01789: ²éѯ¿é¾ßÓв»ÕýÈ·µÄ½á¹ûÁÐÊý
SQL> /*
SQL> ×¢Òâ
SQL> 1. ²ÎÓëÔËËãµÄ¸÷¸ö¼¯ºÏ±ØÐëÁÐÊýÏàͬ ÇÒÀàÐÍÒ»ÖÂ
SQL> 2. ²ÉÓõÚÒ»¸ö¼¯ºÏµÄ±íÍ·×÷Ϊ×îºóµÄ±íÍ·
SQL> 3. Èç¹ûÅÅÐò£¬±ØÐëÔÚÿ¸ö¼¯ºÏºóʹÓÃÏàͬµÄorder by
SQL> 4. ¿ÉÒÔʹÓÃÀ¨ºÅ
SQL> */
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
------ --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 8750
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
20 10875
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
DEPTNO JOB SUM(SAL)
------ --------- ----------
30 9400
29025
ÒÑÑ¡Ôñ13ÐС£
SQL> break on deptno skip 2
SQL> select deptno,job,sum(sal) from emp group by deptno,job
2 union
3 select deptno,to_char(null),sum(sal) from emp group by deptno
4 union
5 select to_number(null),to_char(null),sum(sal) from emp;
DEPTNO JOB SUM(SAL)
------ --------- ----------
10 CLERK 1300
MANAGER 2450
PRESIDENT 5000
8750