Mix n match – LEFT & RIGHT joins

Introduktion till SQL Server

John MacKintosh

Instructor

Varför LEFT- och RIGHT-joins?

  • Varför behöver vi LEFT- och RIGHT-joins?
  • En tabell kanske saknar en exakt matchning i en annan:
    • Kundorderhistorik för marknadsföringskampanj
    • Produktlista och returhistorik
    • Patienter som lagts in men ännu inte skrivits ut
Introduktion till SQL Server

Varför LEFT- och RIGHT-joins?

  • Varför behöver vi LEFT- och RIGHT-joins?
  • En tabell kanske saknar en exakt matchning i en annan:
    • Kundorderhistorik för marknadsföringskampanj
    • Produktlista och returhistorik
    • Patienter som lagts in men ännu inte skrivits ut
Introduktion till SQL Server

Tabellen Admitted

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

Tabellen Discharged

+------------+------------+
| 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       |
+------------+----------+------------+
Introduktion till SQL Server

SYNTAX FÖR LEFT JOIN

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

Introduktion till 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       |
+------------+----------+------------+
Introduktion till SQL Server

RIGHT JOIN

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

Introduktion till SQL Server

Resultat av 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       |
+------------+----------+------------+
Introduktion till SQL Server

Sammanfattning

  • INNER JOIN: Returnerar endast matchande rader
  • LEFT JOIN (eller RIGHT JOIN): Alla rader från huvudtabellen plus matchningar från den kopplade tabellen
  • NULL: Visas när ingen matchning hittas
  • LEFT JOIN och RIGHT JOIN kan användas omväxlande
Introduktion till SQL Server

Introduktion till SQL Server

Nu kör vi en övning!

Introduktion till SQL Server

Preparing Video For Download...