1 / 9

Module 3

Module 3 . Joining Multiple Tables. ◆OverView. 使用別名取代表格名稱 從不同表單組合資料產生單一資料結果 組合不同的資料結果. ◆ Using Aliases for Table Names. 可將 table 用別名取代 使用方法為 AS 可簡化撰寫的程式碼. ◆ Table Joins. Inner Join Outer Join Cross Join Joining More Than Two Table Joining a Table to Itself union. Inner Join.

lyre
Download Presentation

Module 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module 3 Joining Multiple Tables

  2. ◆OverView • 使用別名取代表格名稱 • 從不同表單組合資料產生單一資料結果 • 組合不同的資料結果

  3. ◆Using Aliases for Table Names • 可將table 用別名取代 • 使用方法為AS • 可簡化撰寫的程式碼

  4. ◆ Table Joins • Inner Join • Outer Join • Cross Join • Joining More Than Two Table • Joining a Table to Itself union

  5. Inner Join • 語法:Select * from Table_A inner join Table_B ON條件式 • 依據條件式的原則,將兩個資料表中符合條件式的資料找出。 • Inner join可縮寫成 JOIN • 在on條件式中,指定欄位的寫法為table_name.column_name。 • 搜尋的欄位中,若有同一個欄位名稱,同時出現在兩個資料表中時,也必須寫成table_name.column_name。 • Join的資料表前後順序不重要。 • Join執行順序大於where,因此where所篩選的對象將僅限於join出來的結果。

  6. Outer Join • 語法:select * from Table_A Left Outer Join Table_B on條件式 • Outer Join可再分為Left,Right,Full三種。 • 所取得的資料為:符合on條件式的資料+不符合on條件式的資料。 • 以Left Outer Join 為例: • 符合ON條件式的data 加上 • 以寫在左邊的資料表為主但是不符合on條件式的也要取得。 • Right Outer Join 為Left 的相反 • Full=Right + Left • 可縮寫成Left Join或Right Join或Full Join。 • Outer join前後的順序將會影響搜尋的結果。

  7. Cross Join • 語法:select * from Table_A Cross Join Table_B或者:select * from Table_A , Table_B • Cross Join將把所有的資料表內容相乘後產出,Cross Join沒有On條件式。 • 例如A表有8筆資料,B表有9筆資料,得到的結果將固定有72筆資料產出。 • 建議:非必要時,竟量不要使用Cross Join。以避免程式負擔過大。

  8. Joining More Than Two Table • Select * from Table_A join Table_B on 條件式一 Join Table_C on 條件式二 • 所得到的結果必須同時符合兩組條件式 Joining a Table to Itself union • Select * from Table_A join Table_A on 條件式一

  9. ◆組合不同的資料結果 • 關鍵字UNION • 可將兩個結果集合併為一個 • 兩個結果集的欄位數目必須一樣,對應的欄位型態必須相容。 • 兩個結果集中若有資料是完全一樣的,將會自動過濾掉。 • 若不想過濾相同的資料結果,可使用UNION ALL

More Related