SQL-3-Querying Multiple Tables
SQL-3-Querying Multiple Tables
Self-Joins 自我關聯 ( 主管 - 員工 )
1
Understanding Joins
2
Join Terminology:
Cartesian Product( 笛卡爾乘積 )
Name Product
Davis Alice Mutton
Davis Crab Meat
Name Product
Davis Ipoh Coffee
Davis Alice Mutton Funk Alice Mutton
Funk Crab Meat Funk Crab Meat
King Ipoh Coffee Funk Ipoh Coffee
King Alice Mutton
King Crab Meat
King Ipoh Coffee
3
Overview of Join Types
Cross 笛卡爾乘積
不需要給 ON
4
5
T-SQL Syntax Choices
ANSI SQL-1992
Tables joined by JOIN operator in FROM Clause
Preferred syntax
SELECT ...
FROM Table1 INNER JOIN Table2
ON <on_predicate>
ANSI SQL-1989
Tables joined by commas in FROM Clause
Not recommended: accidental Cartesian products!
SELECT ...
FROM Table1, Table2
WHERE <where_predicate>
6
Querying with Inner Joins
要篩選資料的條件寫在 Where
Self-Join Syntax
Self-Join Examples
10
Self-Join Syntax
Can use same basic structure as inner join, outer join, and cross join
SELECT ...
FROM T1 AS t1 JOIN T1 AS t2
ON t1.Col1 = t2.Col2;
11
Self-Join Examples
列出員工及它的主管編號
12
範例 4.1. - Demonstration A.sql
13