RIGHT JOIN关键字返回右表(table2)中的所有记录,以及左表(table1)中的匹配记录。当没有匹配时,结果从左侧为NULL。
SELECT *
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
完整的表格列表供参考
食物或左表数据
+---------+--------------+-----------+------------+
| ITEM_ID | ITEM_NAME | ITEM_UNIT | COMPANY_ID |
+---------+--------------+-----------+------------+
| 1 | Chex Mix | Pcs | 16 |
| 6 | Cheez-It | Pcs | 15 |
| 2 | BN Biscuit | Pcs | 15 |
| 3 | Mighty Munch | Pcs | 17 |
| 4 | Pot Rice | Pcs | 15 |
| 5 | Jaffa Cakes | Pcs | 18 |
| 7 | Salt n Shake | Pcs | |
+---------+--------------+-----------+------------+
company or RIGHT table data
+------------+---------------+--------------+
| COMPANY_ID | COMPANY_NAME | COMPANY_CITY |
+------------+---------------+--------------+
| 18 | Order All | Boston |
| 15 | Jack Hill Ltd | London |
| 16 | Akas Foods | Delhi |
| 17 | Foodies. | London |
| 19 | sip-n-Bite. | New York |
+------------+---------------+--------------+
SELECT company.company_id,company.company_name,
company.company_city,foods.company_id,foods.item_name
FROM company
RIGHT JOIN foods
ON company.company_id = foods.company_id;
输出
COMPANY_ID COMPANY_NAME COMPANY_CITY COMPANY_ID ITEM_NAME
---------- ------------------------- ------------------------- ---------- --------------
18 Order All Boston 18 Jaffa Cakes
15 Jack Hill Ltd London 15 Pot Rice
15 Jack Hill Ltd London 15 BN Biscuit
15 Jack Hill Ltd London 15 Cheez-It
16 Akas Foods Delhi 16 Chex Mix
17 Foodies. London 17 Mighty Munch
NULL NULL NULL NULL Salt n Shake
更多SQL教程
学习更多SQL教程