Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

PostgreSQL FULL JOIN


FULL JOIN

关键字 FULL JOIN 从两个表中选择所有记录,即使没有匹配项。对于匹配的行,来自两个表的值都可用,如果不存在匹配项,空字段将获取值 NULL

让我们看看使用我们虚拟的 testproducts 表的例子

 testproduct_id |      product_name      | category_id
----------------+------------------------+-------------
              1 | Johns Fruit Cake       |           3
              2 | Marys Healthy Mix      |           9
              3 | Peters Scary Stuff     |          10
              4 | Jims Secret Recipe     |          11
              5 | Elisabeths Best Apples |          12
              6 | Janes Favorite Cheese  |           4
              7 | Billys Home Made Pizza |          13
              8 | Ellas Special Salmon   |           8
              9 | Roberts Rich Spaghetti |           5
             10 | Mias Popular Ice       |          14
(10 行)

我们将尝试将 testproducts 表与 categories 表连接

 category_id | category_name  |                         description
-------------+----------------+------------------------------------------------------------
           1 | Beverages      | 软饮料、咖啡、茶、啤酒和麦酒
           2 | Condiments     | 甜味和咸味酱汁、调味品、酱料和调味料
           3 | Confections    | 甜点、糖果和甜面包
           4 | Dairy Products | 奶酪
           5 | Grains/Cereals | 面包、饼干、意大利面和谷物
           6 | Meat/Poultry   | 加工肉类
           7 | Produce        | 干果和豆腐
           8 | Seafood        | 海藻和鱼
(8 行)

注意: testproducts 中的许多产品都有一个 category_id,它与 categories 表中的任何类别都不匹配。

通过使用 FULL JOIN,我们将从 categories 表和 testproducts 表中获取所有记录

示例

使用 category_id 列将 testproducts 连接到 categories

SELECT testproduct_id, product_name, category_name
FROM testproducts
FULL JOIN categories ON testproducts.category_id = categories.category_id;
运行示例 »

结果

返回来自两个表的全部记录。

没有匹配项的行将在来自对面表的字段中获得 NULL

 testproduct_id |      product_name       | category_name
----------------+-------------------------+----------------
              1 | Johns Fruit Cake        | Confections
              2 | Marys Healthy Mix       |
              3 | Peters Scary Stuff      |
              4 | Jims Secret Recipe      |
              5 | Elisabeths Best Apples  |
              6 | Janes Favorite Cheese   | Dairy Products
              7 | Billys Home Made Pizza  |
              8 | Ellas Special Salmon    | Seafood
              9 | Roberts Rich Spaghetti  | Grains/Cereals
             10 | Mias Popular Ice        |
                |                         | Condiments
                |                         | Meat/Poultry
                |                         | Beverages
                |                         | Produce
(14 行)

注意: FULL JOIN FULL OUTER JOIN 会得到相同的结果。

OUTERFULL JOIN 的默认连接类型,因此,当您编写 FULL JOIN 时,解析器实际上会编写 FULL OUTER JOIN


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.