PostgreSQL 交叉连接(CROSS JOIN)

CROSS JOIN

CROSS JOIN 关键字将 "left" 表中的所有记录和 "right" 表中每个记录进行匹配。

这意味着 "left" 表中的所有记录将针对 "right" 表中每个记录返回。

这种连接方式可能会返回非常大的表,如果不必使用它,就不应该使用它。

让我们看一个使用我们的 testproducts 表的例子:

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

我们将尝试将 testproducts 表与 categories 表连接起来:

  1. category_id | category_name | description
  2. -------------+----------------+------------------------------------------------------------
  3. 1 | Beverages | Soft drinks, coffees, teas, beers, and ales
  4. 2 | Condiments | Sweet and savory sauces, relishes, spreads, and seasonings
  5. 3 | Confections | Desserts, candies, and sweet breads
  6. 4 | Dairy Products | Cheeses
  7. 5 | Grains/Cereals | Breads, crackers, pasta, and cereal
  8. 6 | Meat/Poultry | Prepared meats
  9. 7 | Produce | Dried fruit and bean curd
  10. 8 | Seafood | Seaweed and fish
  11. (8 rows)

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

实例

使用 CROSS JOIN 关键字将 testproductscategories 表关联起来:

  1. SELECT testproduct_id, product_name, category_name
  2. FROM testproducts
  3. CROSS JOIN categories;
结果

将返回每个 testproduct 的所有 categories:

  1. testproduct_id | product_name | category_name
  2. ----------------+------------------------+----------------
  3. 1 | Johns Fruit Cake | Beverages
  4. 1 | Johns Fruit Cake | Condiments
  5. 1 | Johns Fruit Cake | Confections
  6. 1 | Johns Fruit Cake | Dairy Products
  7. 1 | Johns Fruit Cake | Grains/Cereals
  8. 1 | Johns Fruit Cake | Meat/Poultry
  9. 1 | Johns Fruit Cake | Produce
  10. 1 | Johns Fruit Cake | Seafood
  11. 2 | Marys Healthy Mix | Beverages
  12. 2 | Marys Healthy Mix | Condiments
  13. 2 | Marys Healthy Mix | Confections
  14. 2 | Marys Healthy Mix | Dairy Products
  15. 2 | Marys Healthy Mix | Grains/Cereals
  16. 2 | Marys Healthy Mix | Meat/Poultry
  17. 2 | Marys Healthy Mix | Produce
  18. 2 | Marys Healthy Mix | Seafood
  19. 3 | Peters Scary Stuff | Beverages
  20. 3 | Peters Scary Stuff | Condiments
  21. 3 | Peters Scary Stuff | Confections
  22. 3 | Peters Scary Stuff | Dairy Products
  23. 3 | Peters Scary Stuff | Grains/Cereals
  24. 3 | Peters Scary Stuff | Meat/Poultry
  25. 3 | Peters Scary Stuff | Produce
  26. 3 | Peters Scary Stuff | Seafood
  27. 4 | Jims Secret Recipe | Beverages
  28. 4 | Jims Secret Recipe | Condiments
  29. 4 | Jims Secret Recipe | Confections
  30. 4 | Jims Secret Recipe | Dairy Products
  31. 4 | Jims Secret Recipe | Grains/Cereals
  32. 4 | Jims Secret Recipe | Meat/Poultry
  33. 4 | Jims Secret Recipe | Produce
  34. 4 | Jims Secret Recipe | Seafood
  35. 5 | Elisabeths Best Apples | Beverages
  36. 5 | Elisabeths Best Apples | Condiments
  37. 5 | Elisabeths Best Apples | Confections
  38. 5 | Elisabeths Best Apples | Dairy Products
  39. 5 | Elisabeths Best Apples | Grains/Cereals
  40. 5 | Elisabeths Best Apples | Meat/Poultry
  41. 5 | Elisabeths Best Apples | Produce
  42. 5 | Elisabeths Best Apples | Seafood
  43. 6 | Janes Favorite Cheese | Beverages
  44. 6 | Janes Favorite Cheese | Condiments
  45. 6 | Janes Favorite Cheese | Confections
  46. 6 | Janes Favorite Cheese | Dairy Products
  47. 6 | Janes Favorite Cheese | Grains/Cereals
  48. 6 | Janes Favorite Cheese | Meat/Poultry
  49. 6 | Janes Favorite Cheese | Produce
  50. 6 | Janes Favorite Cheese | Seafood
  51. 7 | Billys Home Made Pizza | Beverages
  52. 7 | Billys Home Made Pizza | Condiments
  53. 7 | Billys Home Made Pizza | Confections
  54. 7 | Billys Home Made Pizza | Dairy Products
  55. 7 | Billys Home Made Pizza | Grains/Cereals
  56. 7 | Billys Home Made Pizza | Meat/Poultry
  57. 7 | Billys Home Made Pizza | Produce
  58. 7 | Billys Home Made Pizza | Seafood
  59. 8 | Ellas Special Salmon | Beverages
  60. 8 | Ellas Special Salmon | Condiments
  61. 8 | Ellas Special Salmon | Confections
  62. 8 | Ellas Special Salmon | Dairy Products
  63. 8 | Ellas Special Salmon | Grains/Cereals
  64. 8 | Ellas Special Salmon | Meat/Poultry
  65. 8 | Ellas Special Salmon | Produce
  66. 8 | Ellas Special Salmon | Seafood
  67. 9 | Roberts Rich Spaghetti | Beverages
  68. 9 | Roberts Rich Spaghetti | Condiments
  69. 9 | Roberts Rich Spaghetti | Confections
  70. 9 | Roberts Rich Spaghetti | Dairy Products
  71. 9 | Roberts Rich Spaghetti | Grains/Cereals
  72. 9 | Roberts Rich Spaghetti | Meat/Poultry
  73. 9 | Roberts Rich Spaghetti | Produce
  74. 9 | Roberts Rich Spaghetti | Seafood
  75. 10 | Mias Popular Ice | Beverages
  76. 10 | Mias Popular Ice | Condiments
  77. 10 | Mias Popular Ice | Confections
  78. 10 | Mias Popular Ice | Dairy Products
  79. 10 | Mias Popular Ice | Grains/Cereals
  80. 10 | Mias Popular Ice | Meat/Poultry
  81. 10 | Mias Popular Ice | Produce
  82. 10 | Mias Popular Ice | Seafood
  83. (80 rows)