ORACLE 第4節 多表查詢

來源:互聯網
上載者:User

標籤:

學習目標:

?使用等值和不等值串連在SELECT語句中查詢多個表中的資料。?使用自串連。使用外串連查詢不滿足串連條件的資料


Oracle串連


等值串連:

使用串連在多個表中查詢資料。

SELECT  table1.column, table2.column

FROM  table1, table2

WHERE  table1.column1table2.column2;

?在 WHERE 子句中寫入串連條件。?在表中有相同列時,在列名之前加上表名首碼
兩個表的串連
select e.employee_id,e.last_name,d.department_idfrom employees e,departments dwhere e.department_id = d.department_id
三個表的串連

select e.employee_id,e.last_name,d.department_id,l.cityfrom employees e,departments d,locations lwhere e.department_id = d.department_id andd.location_id = l.location_id

?串連 n個表,至少需要n-1個串連條件。 例如:串連三個表,至少需要兩個串連條件。
非等值串連:

select e.last_name,e.salary,j.grade_levelfrom employees e,job_grades jwhere e.salary between j.lowest_sal and j.highest_sal

內串連和外串連:


內串連: 合并具有同一列的兩個以上的表的行, 結果集中不包含一個表與另一個表不匹配的行外串連: 兩個表在串連過程中除了返回滿足串連條件的行以外還返回左(或右)表中不滿足條件的行,這種串連稱為左(或右)外串連。

沒有匹配的行時, 結果表中相應的列為空白(NULL). 外串連的 WHERE子句條件類似於內部串連, 但串連條件中沒有匹配行的表的列後面要加外串連運算子,即用圓括弧括起來的加號(+). 
--左外串連
select e.employee_id,e.last_name,d.department_namefrom employees e,departments dwhere e.department_id = d.department_id(+)
--右外串連
select e.employee_id,e.last_name,d.department_namefrom employees e,departments dwhere e.department_id(+) = d.department_id

使用SQL: 1999文法串連

使用串連從多個表中查詢資料:

SELECT table1.column, table2.column

FROM  table1

[CROSS JOINtable2] |

[NATURAL JOINtable2] |

[JOINtable2 USING(column_name)] |

[JOINtable2 ON(table1.column_name = table2.column_name)] |

[LEFT|RIGHT|FULL OUTER JOINtable2 ON(table1.column_name =table2.column_name)];

自然串連: ?NATURALJOIN 子句,會以兩個表中具有相同名字的列為條件建立等值串連。?在表中查詢滿足等值條件的資料。如果只是列名相同而資料類型不同,則會產生錯誤。
select e.employee_id,e.last_name,d.department_namefrom employees e natural join departments d

使用 USING子句建立串連:

?在NATURAL JOIN 子句建立等值串連時,可以使用 USING子句指定等值串連中需要用到的列。?使用USING 可以在有多個列滿足條件時進行選擇。

JOIN和USING子句經常同時使用。

select e.employee_id,e.last_name,d.department_namefrom employees e join departments dusing(department_id)

這種方法有局限性:如果兩個表中的列名(一個叫department_id,另外一個叫id)不一樣,這個方法就失效了。

使用ON子句建立串連(常用):

?自然串連中是以具有相同名字的列為串連條件的。?可以使用 ON子句指定額外的串連條件。?這個串連條件是與其它條件分開的。ON子句使語句具有更高的易讀性


<pre name="code" class="sql">select e.employee_id,e.last_name,d.department_namefrom employees e join departments don e.department_id = d.department_id
這個和等值串連是很相似的

select e.employee_id,e.last_name,d.department_name,l.cityfrom employees e <strong>join departments d</strong>on e.department_id = d.department_id<strong>join locations l</strong>on d.location_id = l.location_id


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

ORACLE 第4節 多表查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.