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 CROSS JOIN


交叉连接

CROSS JOIN 关键字将 “左侧” 表中的所有记录与 “右侧” 表中的每条记录匹配。

这意味着将为 “左侧” 表中的每条记录返回 “右侧” 表中的所有记录。

这种连接方式可能返回非常大的表,如果不需要,则不应使用它。

让我们看一个使用我们的虚拟 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 行)

注意: CROSS JOIN 方法将为每个 testproduct 返回所有 categories,这意味着它将返回 80 行 (10 * 8)。

示例

使用 CROSS JOIN 关键字将 testproducts 连接到 categories

SELECT testproduct_id, product_name, category_name
FROM testproducts
CROSS JOIN categories;
运行示例 »

结果

将返回每个 testproduct 的所有 categories

 testproduct_id |      product_name      | category_name
----------------+------------------------+----------------
              1 | Johns Fruit Cake       | Beverages
              1 | Johns Fruit Cake       | Condiments
              1 | Johns Fruit Cake       | Confections
              1 | Johns Fruit Cake       | Dairy Products
              1 | Johns Fruit Cake       | Grains/Cereals
              1 | Johns Fruit Cake       | Meat/Poultry
              1 | Johns Fruit Cake       | Produce
              1 | Johns Fruit Cake       | Seafood
              2 | Marys Healthy Mix      | Beverages
              2 | Marys Healthy Mix      | Condiments
              2 | Marys Healthy Mix      | Confections
              2 | Marys Healthy Mix      | Dairy Products
              2 | Marys Healthy Mix      | Grains/Cereals
              2 | Marys Healthy Mix      | Meat/Poultry
              2 | Marys Healthy Mix      | Produce
              2 | Marys Healthy Mix      | Seafood
              3 | Peters Scary Stuff     | Beverages
              3 | Peters Scary Stuff     | Condiments
              3 | Peters Scary Stuff     | Confections
              3 | Peters Scary Stuff     | Dairy Products
              3 | Peters Scary Stuff     | Grains/Cereals
              3 | Peters Scary Stuff     | Meat/Poultry
              3 | Peters Scary Stuff     | Produce
              3 | Peters Scary Stuff     | Seafood
              4 | Jims Secret Recipe     | Beverages
              4 | Jims Secret Recipe     | Condiments
              4 | Jims Secret Recipe     | Confections
              4 | Jims Secret Recipe     | Dairy Products
              4 | Jims Secret Recipe     | Grains/Cereals
              4 | Jims Secret Recipe     | Meat/Poultry
              4 | Jims Secret Recipe     | Produce
              4 | Jims Secret Recipe     | Seafood
              5 | Elisabeths Best Apples | Beverages
              5 | Elisabeths Best Apples | Condiments
              5 | Elisabeths Best Apples | Confections
              5 | Elisabeths Best Apples | Dairy Products
              5 | Elisabeths Best Apples | Grains/Cereals
              5 | Elisabeths Best Apples | Meat/Poultry
              5 | Elisabeths Best Apples | Produce
              5 | Elisabeths Best Apples | Seafood
              6 | Janes Favorite Cheese  | Beverages
              6 | Janes Favorite Cheese  | Condiments
              6 | Janes Favorite Cheese  | Confections
              6 | Janes Favorite Cheese  | Dairy Products
              6 | 珍妮丝最爱的奶酪  | 谷物/谷类
              6 | 珍妮丝最爱的奶酪  | 肉类/家禽
              6 | 珍妮丝最爱的奶酪  | 生鲜农产品
              6 | 珍妮丝最爱的奶酪  | 海鲜
              7 | 比利的家制比萨  | 饮料
              7 | 比利的家制比萨  | 调味品
              7 | 比利的家制比萨  | 糖果
              7 | 比利的家制比萨  | 乳制品
              7 | 比利的家制比萨  | 谷物/谷类
              7 | 比利的家制比萨  | 肉类/家禽
              7 | 比利的家制比萨  | 生鲜农产品
              7 | 比利的家制比萨  | 海鲜
              8 | 埃拉的特色三文鱼  | 饮料
              8 | 埃拉的特色三文鱼  | 调味品
              8 | 埃拉的特色三文鱼  | 糖果
              8 | 埃拉的特色三文鱼  | 乳制品
              8 | 埃拉的特色三文鱼  | 谷物/谷类
              8 | 埃拉的特色三文鱼  | 肉类/家禽
              8 | 埃拉的特色三文鱼  | 生鲜农产品
              8 | 埃拉的特色三文鱼  | 海鲜
              9 | 罗伯特的浓郁意式面条 | 饮料
              9 | 罗伯特的浓郁意式面条 | 调味品
              9 | 罗伯特的浓郁意式面条 | 糖果
              9 | 罗伯特的浓郁意式面条 | 乳制品
              9 | 罗伯特的浓郁意式面条 | 谷物/谷类
              9 | 罗伯特的浓郁意式面条 | 肉类/家禽
              9 | 罗伯特的浓郁意式面条 | 生鲜农产品
              9 | 罗伯特的浓郁意式面条 | 海鲜
             10 | 米娅的畅销冰淇淋   | 饮料
             10 | 米娅的畅销冰淇淋   | 调味品
             10 | 米娅的畅销冰淇淋   | 糖果
             10 | 米娅的畅销冰淇淋   | 乳制品
             10 | 米娅的畅销冰淇淋   | 谷物/谷类
             10 | 米娅的畅销冰淇淋   | 肉类/家禽
             10 | 米娅的畅销冰淇淋   | 生鲜农产品
             10 | 米娅的畅销冰淇淋   | 海鲜
(80 行)

PostgreSQL 练习

通过练习测试自己

练习

如果我们使用 CROSS JOIN 连接一个有 10 条记录的表和一个有 8 条记录的表,将返回多少行?

The number of returned records will be: 
        

开始练习


×

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.