Mastering Oracle SQL学习笔记(join句法专题第三部份)

2.4 几种类型的 Join 句法
     
2.4.1 Cross Joins : 该类型的 Join 句法不需要连接条件,一个表中的所有行分别都会连接另一个表的所有行,得到的结果被称为一个笛卡尔积:
      employee 表中有 14 行,在 department 表中有 4 ,14*4 56
 
      SELECT e.lname, d.name
 
FROM employee e CROSS JOIN department d;
 
LNAME      NAME
 
---------- --------------
 
SMITH      ACCOUNTING
 
ALLEN      ACCOUNTING
 
WARD       ACCOUNTING
 
JONES      ACCOUNTING
 
MARTIN     ACCOUNTING
 
BLAKE      ACCOUNTING
 
 . . .
 
 . . .
 
 . . .
 
SCOTT      OPERATIONS
 
KING       OPERATIONS
 
TURNER     OPERATIONS
 
ADAMS      OPERATIONS
 
JAMES      OPERATIONS
 
FORD       OPERATIONS
 
MILLER     OPERATIONS
 
 
 
56 rows selected.
 
 
注意, Join 句法中若没有ON关键字是错误的,使用Cross Join句法时不要用ON关键字,否则也是错误的:
1 SELECT e.lname, d.name
 
FROM employee e JOIN department d;
 
FROM employee e JOIN department d
 
ERROR at line 2:
 
ORA-00905: missing keyword
 
2 SELECT e.lname, d.name
 
FROM employee e CROSS JOIN department d
 
ON e.dept_id = d.dept_id;
 
ON e.dept_id = d.dept_id
 
*
 
ERROR at line 3:
 
ORA-00933: SQL command not properly ended
 
<!--[if !supportLists]--> 2.4.2    <!--[endif]--> Inner Join inner join 其实就是常规的 join 前面介绍过了,这里就不哆嗦了!
<!--[if !supportLists]--> 2.4.3    <!--[endif]--> Outer Join : 有时候你想把一个表中的所有行全都取出来,即使在另一个表中没有数据与该表中的某些行对应。那么你可以用 outer join.
Outer Join 的句法:
FROM table1 { LEFT | RIGHT | FULL } [OUTER] JOIN table2
 
LEFT
      取出table1 中的所有行,如果在table2 中没有数据与之对应,则为null;
例:
SELECT d.dept_id, d.name, l.regional_group
 
FROM department d LEFT OUTER JOIN location l
 
ON d.location_id = l.location_id;
 
 
 
   DEPT_ID NAME                 REGIONAL_GROUP
 
---------- -------------------- --------------
 
        10 ACCOUNTING           NEW YORK
 
        20 RESEARCH             DALLAS
 
        30 SALES
 
        40 OPERATIONS           BOSTON
 
RIGHT:
      取出table2 中的所有行,如果在table1 中没有数据与之对应,则为null;
例:
SELECT d.dept_id, d.name, l.regional_group
 
FROM department d RIGHT OUTER JOIN location l
 
ON d.location_id = l.location_id;
 
 
 
   DEPT_ID NAME                 REGIONAL_GROUP
 
    ---------- -------------------- ---------------
        10  ACCOUNTING        NEW YORK
 
                                                CHICAGO
 
        20 RESEARCH             DALLAS
 
                                                SAN FRANCISCO
 
        40 OPERATIONS           BOSTON
 
 
 
FULL
取出table1 中的所有行又要取出table2 中的所有行,同时table1 table2 关联,table1 table2 相互之间无数据关联的列为null;
                 例:
                 SELECT d.dept_id, d.name, l.regional_group
 
FROM department d FULL OUTER JOIN location l
 
ON d.location_id = l.location_id;
 
 
 
                DEPT_ID NAME                 REGIONAL_GROUP
 
---------- -------------------- ----------------
 
              10 ACCOUNTING           NEW YORK
 
              20 RESEARCH                 DALLAS
 
              30 SALES
 
              40 OPERATIONS               BOSTON
 
                                                           CHICAGO
 
                                                           SAN FRANCISCO
 
 
 
6 rows selected.

你可能感兴趣的:(oracle,数据库,职场,休闲)