混搭:LEFT 與 RIGHT 連接

SQL Server 入門

John MacKintosh

Instructor

使用 LEFT 與 RIGHT 連接的理由

  • 為什麼需要 LEFTRIGHT 連接?
  • 兩張表可能無法一一對應:
    • 行銷活動用的顧客訂單紀錄
    • 產品清單與退貨紀錄
    • 病人已收治但尚未出院
SQL Server 入門

使用 LEFT 與 RIGHT 連接的理由

  • 為什麼需要 LEFTRIGHT 連接?
  • 兩張表可能無法一一對應:
    • 行銷活動用的顧客訂單紀錄
    • 產品清單與退貨紀錄
    • 病人已收治但尚未出院
SQL Server 入門

Admissions 表

+------------+----------+
| Patient_ID | Admitted | 
|------------+----------|
| 1          | 1        |
| 2          | 1        |
| 3          | 1        |
| 4          | 1        |
| 5          | 1        |
+------------+----------+

Discharges 表

+------------+------------+
| Patient_ID | Discharged | 
|------------+------------|
| 1          | 1          |
| 3          | 1          |
| 4          | 1          |
+------------+------------+

INNER JOIN:

+------------+----------+------------|
| Patient_ID | Admitted | Discharged |
|------------+----------|------------|
| 1          | 1        | 1          |
| 3          | 1        | 1          |
| 4          | 1        | 1          |
+------------+----------+------------+

LEFT JOIN:

+------------+----------+------------|
| Patient_ID | Admitted | Discharged |
|------------+----------|------------|
| 1          | 1        | 1          |
| 2          | 1        | NULL       |
| 3          | 1        | 1          |
| 4          | 1        | 1          |
| 5          | 1        | NULL       |
+------------+----------+------------+
SQL Server 入門

LEFT JOIN 語法

SELECT 
  Admitted.Patient_ID, 
  Admitted, 
  Discharged 
FROM Admitted 
LEFT JOIN Discharged ON Discharged.Patient_ID = Admitted.Patient_ID;

SQL Server 入門
SELECT 
  Admitted.Patient_ID, 
  Admitted, 
  Discharged 
FROM Admitted 
LEFT JOIN Discharged ON Discharged.Patient_ID = Admitted.Patient_ID;
+------------+----------+------------|
| Patient_ID | Admitted | Discharged |
|------------+----------|------------|
| 1          | 1        | 1          |
| 2          | 1        | NULL       |
| 3          | 1        | 1          |
| 4          | 1        | 1          |
| 5          | 1        | NULL       |
+------------+----------+------------+
SQL Server 入門

RIGHT JOIN

SELECT 
  Admitted.Patient_ID, 
  Admitted, 
  Discharged 
FROM Discharged 
RIGHT JOIN Admitted ON Admitted.Patient_ID = Discharged.Patient_ID;

SQL Server 入門

RIGHT JOIN 結果

SELECT 
  Admitted.Patient_ID, 
  Admitted, 
  Discharged 
FROM Discharged 
RIGHT JOIN Admitted ON Admitted.Patient_ID = Discharged.Patient_ID;
+------------+----------+------------|
| Patient_ID | Admitted | Discharged |
|------------+----------|------------|
| 1          | 1        | 1          |
| 2          | 1        | NULL       |
| 3          | 1        | 1          |
| 4          | 1        | 1          |
| 5          | 1        | NULL       |
+------------+----------+------------+
SQL Server 入門

重點整理

  • INNER JOIN:只回傳可配對的列
  • LEFT JOIN(或 RIGHT JOIN):主表的所有列加上可配對的連接表列
  • NULL:找不到配對時顯示
  • LEFT JOINRIGHT JOIN 可互換使用
SQL Server 入門

SQL Server 入門

一起來練習吧!

SQL Server 入門

Preparing Video For Download...