多表的連線

zhouxianwang發表於2013-03-11
多表連線:

SQL> select * from dept;

    DEPTNO DNAME                                      LOC
---------- ------------------------------------------ ---------------------------------------
        10 ACCOUNTING                                 NEW YORK
        20 RESEARCH                                   DALLAS
        30 SALES                                      CHICAGO
        40 OPERATIONS                                 BOSTON

SQL> select  * from emp;

     EMPNO ENAME                JOB               MGR HIREDATE               SAL       COMM     DEPTNO
---------- -------------------- ---------- ---------- --------------- ---------- ---------- ----------
      7890 zxw                  clerk            7902 11-NOV-80              900        100
      7892 test                 clerk            7906 22-NOV-82              860         50
      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

     EMPNO ENAME                JOB               MGR HIREDATE               SAL       COMM     DEPTNO
---------- -------------------- ---------- ---------- --------------- ---------- ---------- ----------
      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
      7934 MILLER               CLERK            7782 23-JAN-82             1300                    10

16 rows selected.

1.笛卡爾連線:


SQL> select count(1) from dept ,emp;

  COUNT(1)
----------
        64

這個資料沒任何意議,沒有連線條件;


2.內連線;

等值關係:
做多表連線,一定要找關聯關係,主外來鍵找關聯關係;



SQL> select e.ename,e.deptno,d.dname from emp e,dept d
  2  where e.deptno=d.deptno;

ENAME                    DEPTNO DNAME
-------------------- ---------- ------------------------------------------
KING                         10 ACCOUNTING
CLARK                        10 ACCOUNTING
MILLER                       10 ACCOUNTING
FORD                         20 RESEARCH
SCOTT                        20 RESEARCH
ADAMS                        20 RESEARCH
JONES                        20 RESEARCH
SMITH                        20 RESEARCH
TURNER                       30 SALES
MARTIN                       30 SALES
WARD                         30 SALES

ENAME                    DEPTNO DNAME
-------------------- ---------- ------------------------------------------
ALLEN                        30 SALES
BLAKE                        30 SALES
JAMES                        30 SALES

14 rows selected.

----內連線,標準的SQL,支援所有的SQL
SQL> select e.ename,e.deptno,d.dname from emp e,dept d
  2  where e.deptno=d.deptno;
 
  也可寫成:
 
           SQL> select e.ename,e.deptno,d.dname
           2     from emp e inner join dept d  on e.deptno=d.deptno;
        
         ENAME                    DEPTNO DNAME
         -------------------- ---------- ------------------------------------------
         KING                         10 ACCOUNTING
         CLARK                        10 ACCOUNTING
         MILLER                       10 ACCOUNTING
         FORD                         20 RESEARCH
         SCOTT                        20 RESEARCH
         ADAMS                        20 RESEARCH
         JONES                        20 RESEARCH
         SMITH                        20 RESEARCH
         TURNER                       30 SALES
         MARTIN                       30 SALES
         WARD                         30 SALES
        
         ENAME                    DEPTNO DNAME
         -------------------- ---------- ------------------------------------------
         ALLEN                        30 SALES
         BLAKE                        30 SALES
         JAMES                        30 SALES
        
         14 rows selected.



3.外連線;
    在外連線中,某些不滿足條件的列也會顯示出來。
    也就是說,要限制其中一個表的行,而不是限制另一個表的行。
    這種連線形式在許多情況下是非常有用的。
     在外連線中又分左連線和右連線
    
   A.左連線:
         ORACLE的寫法:+號右邊,以左邊為準,右邊補齊。
       左連線+在右邊,左邊的是主表,左邊的表記錄 全部顯示,
       如果左邊的表有記錄,右邊的表沒有找到記錄,則補NULL.
    



SQL> select e.ename, e.job,e.sal,d.deptno,d.dname,d.loc
  2  from  emp e, dept d 
  3  where e.deptno=d.deptno(+);
 
 
 
     SQL>  select e.ename, e.job,e.sal,d.deptno,d.dname,d.loc
                      from  emp e  left outer join  dept d 
                       on e.deptno=d.deptno;

ENAME      JOB               SAL     DEPTNO DNAME                LOC
---------- ---------- ---------- ---------- -------------------- ---------------------------------------
MILLER     CLERK            1300         10 ACCOUNTING           NEW YORK
KING       PRESIDENT        5000         10 ACCOUNTING           NEW YORK
CLARK      MANAGER          2450         10 ACCOUNTING           NEW YORK
FORD       ANALYST          3000         20 RESEARCH             DALLAS
ADAMS      CLERK            1100         20 RESEARCH             DALLAS
SCOTT      ANALYST          3000         20 RESEARCH             DALLAS
JONES      MANAGER          2975         20 RESEARCH             DALLAS
SMITH      CLERK             800         20 RESEARCH             DALLAS
JAMES      CLERK             950         30 SALES                CHICAGO
TURNER     SALESMAN         1500         30 SALES                CHICAGO
BLAKE      MANAGER          2850         30 SALES                CHICAGO

ENAME      JOB               SAL     DEPTNO DNAME                LOC
---------- ---------- ---------- ---------- -------------------- ---------------------------------------
MARTIN     SALESMAN         1250         30 SALES                CHICAGO
WARD       SALESMAN         1250         30 SALES                CHICAGO
ALLEN      SALESMAN         1600         30 SALES                CHICAGO
test       clerk             860                                              ----------多的
zxw        clerk             900                                            ---------多的   

16 rows selected.



   B.右連線:
         ORACLE的寫法:+號左邊,以右邊為準,左邊補齊。
       右連線+在左邊,右邊的是主表,右邊的表記錄 全部顯示,
       如果右邊的表有記錄,左邊的表沒有找到記錄,則補NULL.
      
      
      
      
      
       SQL>  select e.ename, e.job,e.sal,d.deptno,d.dname,d.loc
                        from  emp e  , dept d 
                       where e.deptno(+)=d.deptno;
 
 
 
         SQL>  select e.ename, e.job,e.sal,d.deptno,d.dname,d.loc
                      from  emp e  right outer join  dept d 
                       on e.deptno=d.deptno;
 
 

ENAME      JOB               SAL     DEPTNO DNAME                LOC
---------- ---------- ---------- ---------- -------------------- ---------------------------------------
KING       PRESIDENT        5000         10 ACCOUNTING           NEW YORK
CLARK      MANAGER          2450         10 ACCOUNTING           NEW YORK
MILLER     CLERK            1300         10 ACCOUNTING           NEW YORK
FORD       ANALYST          3000         20 RESEARCH             DALLAS
SCOTT      ANALYST          3000         20 RESEARCH             DALLAS
ADAMS      CLERK            1100         20 RESEARCH             DALLAS
JONES      MANAGER          2975         20 RESEARCH             DALLAS
SMITH      CLERK             800         20 RESEARCH             DALLAS
TURNER     SALESMAN         1500         30 SALES                CHICAGO
MARTIN     SALESMAN         1250         30 SALES                CHICAGO
WARD       SALESMAN         1250         30 SALES                CHICAGO

ENAME      JOB               SAL     DEPTNO DNAME                LOC
---------- ---------- ---------- ---------- -------------------- ---------------------------------------
ALLEN      SALESMAN         1600         30 SALES                CHICAGO
BLAKE      MANAGER          2850         30 SALES                CHICAGO
JAMES      CLERK             950         30 SALES                CHICAGO
       多的   ----------                       40 OPERATIONS           BOSTON

15 rows selected.
 
 C.自連線,左右都顯示:
 
 
select e.ename, e.job,e.sal,d.deptno,d.dname,d.loc
from  emp e  full outer join  dept d
on e.deptno=d.deptno;
 
  SQL> select e.ename, e.job,e.sal,d.deptno,d.dname,d.loc
  2  from  emp e  full outer join  dept d
  3  on e.deptno=d.deptno;

ENAME      JOB               SAL     DEPTNO DNAME                LOC
---------- ---------- ---------- ---------- -------------------- ---------------------------------------
MILLER     CLERK            1300         10 ACCOUNTING           NEW YORK
KING       PRESIDENT        5000         10 ACCOUNTING           NEW YORK
CLARK      MANAGER          2450         10 ACCOUNTING           NEW YORK
FORD       ANALYST          3000         20 RESEARCH             DALLAS
ADAMS      CLERK            1100         20 RESEARCH             DALLAS
SCOTT      ANALYST          3000         20 RESEARCH             DALLAS
JONES      MANAGER          2975         20 RESEARCH             DALLAS
SMITH      CLERK             800         20 RESEARCH             DALLAS
JAMES      CLERK             950         30 SALES                CHICAGO
TURNER     SALESMAN         1500         30 SALES                CHICAGO
BLAKE      MANAGER          2850         30 SALES                CHICAGO

ENAME      JOB               SAL     DEPTNO DNAME                LOC
---------- ---------- ---------- ---------- -------------------- ---------------------------------------
MARTIN     SALESMAN         1250         30 SALES                CHICAGO
WARD       SALESMAN         1250         30 SALES                CHICAGO
ALLEN      SALESMAN         1600         30 SALES                CHICAGO
test       clerk             860
zxw        clerk             900
                                         40 OPERATIONS           BOSTON

17 rows selected.
 
      

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27036311/viewspace-755748/,如需轉載,請註明出處,否則將追究法律責任。

相關文章