摘要:在本教程中,您將學習如何使用 SQL RIGHT JOIN 子句合併兩個表中的行。
SQL RIGHT JOIN 子句簡介 #
RIGHT JOIN 是 SELECT 語句的一個可選子句。RIGHT JOIN 子句允許您合併兩個表中的行。
以下是 RIGHT JOIN 子句的語法:
SELECT
column1,
column2
FROM
left_table
RIGHT JOIN right_table ON condition;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)在此語法中:
- 首先,在
FROM子句中指定第一個表(left_table)。 - 其次,在
RIGHT JOIN子句中提供第二個表(right_table)。 - 第三,在
ON關鍵字後指定第一個表和第二個表之間行匹配的條件。
對於 right_table 中的每一行,RIGHT JOIN 會與 left_table 中的每一行檢查條件。如果條件為 true,RIGHT JOIN 會將兩個表中的兩行合併為一行。
如果條件為 false,RIGHT JOIN 子句會將右表中的行與一個由 NULL 值填充的左錶行合併成一個單行。換句話說,RIGHT JOIN 子句總是會包含右表中的所有行。
通常,您會使用等於運算子 (=) 來比較左右兩個表的列之間的行。
SELECT
column1,
column2
FROM
left_table
RIGHT JOIN right_table ON right_table.column1 = left_table_column2;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)RIGHT JOIN 和 RIGHT OUTER JOIN 是相同的,因為 OUTER 關鍵字是可選的。
理解 SQL right join #
假設您有兩個表:
X表有兩個列:id(鍵) 和x。Y表也有兩列:id(鍵)和y。
右連線使用兩個表中 id 列的值來匹配 X 表和 Y 表之間的行。
右連線包含右表(Y)中的所有行以及左表(X)中的匹配行;如果沒有匹配的行,它將對左表(X)的列使用 null。
下面的維恩圖是說明右連線的另一種方式。
基本的 SQL RIGHT JOIN 子句示例 #
假設我們有兩個表:employees 和 departments。
employees 表
| employee_id | name | department_id |
|---|---|---|
| 1 | Jane | 1 |
| 2 | Bob | 2 |
| 3 | Maria | NULL |
departments 表
| department_id | department_name |
|---|---|
| 1 | Sales |
| 2 | 市場營銷 |
以下查詢使用 RIGHT JOIN 子句從 employees 和 departments 表中檢索員工 ID、姓名和部門名稱。
SELECT
employee_id,
name,
department_name
FROM
departments
RIGHT JOIN employees ON employees.department_id = departments.department_id;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)輸出
employee_id | name | department_name
-------------+-------+-----------------
1 | Jane | Sales
2 | Bob | Marketing
3 | Maria | NULLCode language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)查詢工作原理
第 1 步:RIGHT JOIN 子句檢查 employees 表中的每一行。
行 #1 (department_id 1) 與 departments 表中的行 #1 (department_id 1) 匹配。RIGHT JOIN 子句將兩個表中的行合併為一行。
| employee_id | name | department_id | department_id | department_name |
|---|---|---|---|---|
| 1 | Jane | 1 | 1 | Sales |
行 #2 (department_id 2) 與 departments 表中的行 #2 (department_id 2) 匹配。RIGHT JOIN 子句將這兩行合併為一個新行。
| employee_id | name | department_id | department_id | department_name |
|---|---|---|---|---|
| 2 | Bob | 2 | 2 | 市場營銷 |
行 #3 (department_id 為 NULL) 與 departments 表中的任何行都不匹配。RIGHT JOIN 包含 employees 表中的這一行,併為 departments 表的行所對應的列填充 NULL。
| employee_id | name | department_id | department_id | department_name |
|---|---|---|---|---|
| 3 | Maria | NULL | NULL | NULL |
RIGHT JOIN 得出以下中間結果集:
| employee_id | name | department_id | department_id | department_name |
|---|---|---|---|---|
| 1 | Jane | 1 | 1 | Sales |
| 2 | Bob | 2 | 2 | 市場營銷 |
| 3 | Maria | NULL | NULL | NULL |
第 2 步:SELECT 子句從中間結果集中檢索 employee_id、name 和 department_id,並返回以下最終結果集:
| employee_id | name | department_id |
|---|---|---|
| 1 | Jane | 1 |
| 2 | Bob | 2 |
| 3 | Maria | NULL |
使用表別名 #
當使用 RIGHT JOIN 子句將一個表與另一個表連線時,您可以使用表別名。例如:
SELECT
employee_id,
name,
department_name
FROM
departments d
RIGHT JOIN employees e ON e.department_id = d.department_id;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)查詢左表中不匹配的行 #
RIGHT JOIN 子句可以幫助使用以下查詢模式在右表中查詢不匹配的行:
SELECT
column1,
column2
FROM
left_table
RIGHT JOIN right_table ON right_table.column1 = left_table_column2
WHERE column2 IS NULL;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)WHERE 子句中的條件保留了 column2 列值為 NULL 的行。這些行在左表中沒有匹配的行。
例如,您可以使用 RIGHT JOIN 子句來查詢不屬於任何部門的員工。
SELECT
employee_id,
name,
department_name
FROM
departments d
RIGHT JOIN employees e ON e.department_id = d.department_id
WHERE
department_name IS NULL;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)輸出
employee_id | name | department_name
-------------+-------+-----------------
3 | Maria | NULLCode language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)使用 RIGHT JOIN 子句連線兩個表 #
我們將使用 HR 示例資料庫中的 countries 和 locations 表。

以下查詢使用 RIGHT JOIN 子句從 countries 和 locations 表中檢索 country_name 和 city。
SELECT
country_name,
city
FROM
locations l
RIGHT JOIN countries c ON c.country_id = l.country_id
ORDER BY
city,
country_name;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)輸出
country_name | city
--------------------------+---------------------
United Kingdom | London
Germany | Munich
United Kingdom | Oxford
United States of America | Seattle
United States of America | South San Francisco
United States of America | Southlake
Canada | Toronto
Argentina | NULL
Australia | NULL
...Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)結果集包括所有的國家名稱(來自 countries 表)和城市(來自 locations 表)。對於在 locations 表中沒有城市的國家,城市為 NULL。
使用 RIGHT JOIN 子句連線三個表 #
這裡是來自 HR 示例資料庫的三個表:regions、countries 和 locations。

以下查詢使用 RIGHT JOIN 子句從這三個表中檢索資料。
SELECT
region_name,
country_name,
city
FROM
locations l
RIGHT JOIN countries c ON c.country_id = l.country_id
RIGHT JOIN regions r ON r.region_id = c.region_id
ORDER BY
region_name;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)輸出
region_name | country_name | city
------------------------+--------------------------+---------------------
Americas | United States of America | South San Francisco
Americas | United States of America | Southlake
Americas | Mexico | NULL
Americas | Canada | Toronto
Americas | Brazil | NULL
Americas | United States of America | Seattle
Americas | Argentina | NULL
Asia | Australia | NULL
Asia | HongKong | NULL
...Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)摘要 #
- 使用
RIGHT JOIN子句來選擇行以合併兩個表中的資料。RIGHT JOIN子句總是在結果集中包含右表的所有行。 - 使用
RIGHT JOIN子句來查詢右表中不匹配的行。
測驗 #
資料庫 #
- PostgreSQL RIGHT JOIN 子句
- MySQL RIGHT JOIN 子句
- SQLite RIGHT JOIN 子句
- Db2 RIGHT JOIN 子句
- Oracle RIGHT JOIN 子句
- SQL Server RIGHT JOIN 子句