这个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 rows)
我们将努力加入testproducts
表与categories
表格:
category_id | category_name | description
-------------+----------------+------------------------------------------------------------
1 | Beverages | Soft drinks, coffees, teas, beers, and ales
2 | Condiments | Sweet and savory sauces, relishes, spreads, and seasonings
3 | Confections | Desserts, candies, and sweet breads
4 | Dairy Products | Cheeses
5 | Grains/Cereals | Breads, crackers, pasta, and cereal
6 | Meat/Poultry | Prepared meats
7 | Produce | Dried fruit and bean curd
8 | Seafood | Seaweed and fish
(8 rows)
笔记:许多产品在testproducts
有一个category_id
与中的任何类别都不匹配categories
表格。
通过使用FULL JOIN
我们将从两个地方获取所有记录categories
表和testproducts
表格:
加入testproducts
到 categories
使用category_id
柱子:
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 rows)
笔记:FULL JOIN
和 FULL OUTER JOIN
会给出相同的结果。
OUTER
是默认的连接类型FULL JOIN
,所以当你写FULL JOIN
解析器实际上写的是FULL OUTER JOIN
。
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!