{"id":2654,"date":"2025-03-12T23:32:06","date_gmt":"2025-03-12T15:32:06","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2654"},"modified":"2025-03-12T23:32:06","modified_gmt":"2025-03-12T15:32:06","slug":"postgresql-%e8%bf%9e%e6%8e%a5%ef%bc%88join%ef%bc%89%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/12\/postgresql-%e8%bf%9e%e6%8e%a5%ef%bc%88join%ef%bc%89%e8%af%a6%e8%a7%a3\/","title":{"rendered":"PostgreSQL \u8fde\u63a5\uff08JOIN\uff09\u8be6\u89e3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 PostgreSQL \u4e2d\uff0c<code>JOIN<\/code> \u7528\u4e8e\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u8868\u7684\u6570\u636e\u5173\u8054\u8d77\u6765\uff0c\u6839\u636e\u7279\u5b9a\u7684\u6761\u4ef6\u7ec4\u5408\u6570\u636e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. JOIN \u7684\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL \u63d0\u4f9b\u4e86\u591a\u79cd <code>JOIN<\/code> \u7c7b\u578b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>INNER JOIN<\/code>\uff08\u5185\u8fde\u63a5\uff09<\/li>\n\n\n\n<li><code>LEFT JOIN<\/code>\uff08\u5de6\u8fde\u63a5\uff09<\/li>\n\n\n\n<li><code>RIGHT JOIN<\/code>\uff08\u53f3\u8fde\u63a5\uff09<\/li>\n\n\n\n<li><code>FULL JOIN<\/code>\uff08\u5168\u8fde\u63a5\uff09<\/li>\n\n\n\n<li><code>CROSS JOIN<\/code>\uff08\u4ea4\u53c9\u8fde\u63a5\uff09<\/li>\n\n\n\n<li><code>SELF JOIN<\/code>\uff08\u81ea\u8fde\u63a5\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. INNER JOIN\uff08\u5185\u8fde\u63a5\uff09<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>INNER JOIN<\/code> \u4ec5\u8fd4\u56de\u4e24\u4e2a\u8868\u4e2d <strong>\u5339\u914d<\/strong> \u7684\u6570\u636e\u884c\uff0c\u4e0d\u5339\u914d\u7684\u6570\u636e\u4e0d\u4f1a\u51fa\u73b0\u5728\u7ed3\u679c\u96c6\u4e2d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT A.*, B.*\nFROM table_A A\nINNER JOIN table_B B\nON A.common_column = B.common_column;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6709\u4e24\u4e2a\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE customers (\n    id SERIAL PRIMARY KEY,\n    name VARCHAR(100)\n);\n\nCREATE TABLE orders (\n    id SERIAL PRIMARY KEY,\n    customer_id INT,\n    product VARCHAR(100),\n    FOREIGN KEY (customer_id) REFERENCES customers(id)\n);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u63d2\u5165\u6d4b\u8bd5\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO customers (id, name) VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie');\nINSERT INTO orders (id, customer_id, product) VALUES (1, 1, 'Laptop'), (2, 2, 'Phone');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c <code>INNER JOIN<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT customers.name, orders.product\nFROM customers\nINNER JOIN orders\nON customers.id = orders.customer_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>name<\/th><th>product<\/th><\/tr><\/thead><tbody><tr><td>Alice<\/td><td>Laptop<\/td><\/tr><tr><td>Bob<\/td><td>Phone<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Charlie<\/code> \u6ca1\u6709\u8ba2\u5355\uff0c\u56e0\u6b64\u4e0d\u4f1a\u51fa\u73b0\u5728\u7ed3\u679c\u4e2d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. LEFT JOIN\uff08\u5de6\u8fde\u63a5\uff09<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>LEFT JOIN<\/code> \u8fd4\u56de\u5de6\u8868\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u5373\u4f7f\u53f3\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT A.*, B.*\nFROM table_A A\nLEFT JOIN table_B B\nON A.common_column = B.common_column;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT customers.name, orders.product\nFROM customers\nLEFT JOIN orders\nON customers.id = orders.customer_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>name<\/th><th>product<\/th><\/tr><\/thead><tbody><tr><td>Alice<\/td><td>Laptop<\/td><\/tr><tr><td>Bob<\/td><td>Phone<\/td><\/tr><tr><td>Charlie<\/td><td>NULL<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5373\u4f7f <code>Charlie<\/code> \u6ca1\u6709\u8ba2\u5355\uff0c\u4ed6\u4ecd\u7136\u51fa\u73b0\u5728\u7ed3\u679c\u4e2d\uff0c<code>product<\/code> \u5217\u663e\u793a <code>NULL<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. RIGHT JOIN\uff08\u53f3\u8fde\u63a5\uff09<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>RIGHT JOIN<\/code> \u8fd4\u56de\u53f3\u8868\u7684\u6240\u6709\u6570\u636e\uff0c\u5373\u4f7f\u5de6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT A.*, B.*\nFROM table_A A\nRIGHT JOIN table_B B\nON A.common_column = B.common_column;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT customers.name, orders.product\nFROM customers\nRIGHT JOIN orders\nON customers.id = orders.customer_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u4e8e <code>orders<\/code> \u8868\u4e2d\u6240\u6709\u6570\u636e\u90fd\u5339\u914d <code>customers<\/code>\uff0c\u7ed3\u679c\u4e0e <code>INNER JOIN<\/code> \u76f8\u540c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c <code>orders<\/code> \u8868\u6709\u6570\u636e\u4f46 <code>customers<\/code> \u6ca1\u6709\u5339\u914d\u7684 <code>id<\/code>\uff0c\u4e5f\u4f1a\u8fd4\u56de\u6570\u636e\uff0c\u4f46 <code>customers.name<\/code> \u4f1a\u663e\u793a <code>NULL<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. FULL JOIN\uff08\u5168\u8fde\u63a5\uff09<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>FULL JOIN<\/code> \u8fd4\u56de\u5de6\u8868\u548c\u53f3\u8868\u7684\u6240\u6709\u6570\u636e\uff0c\u82e5\u6ca1\u6709\u5339\u914d\u7684\u90e8\u5206\uff0c\u5219\u8fd4\u56de <code>NULL<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.1 \u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT A.*, B.*\nFROM table_A A\nFULL JOIN table_B B\nON A.common_column = B.common_column;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.2 \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT customers.name, orders.product\nFROM customers\nFULL JOIN orders\nON customers.id = orders.customer_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>name<\/th><th>product<\/th><\/tr><\/thead><tbody><tr><td>Alice<\/td><td>Laptop<\/td><\/tr><tr><td>Bob<\/td><td>Phone<\/td><\/tr><tr><td>Charlie<\/td><td>NULL<\/td><\/tr><tr><td>NULL<\/td><td>Tablet<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c <code>orders<\/code> \u8868\u4e2d\u6709\u8ba2\u5355\u5c5e\u4e8e\u4e00\u4e2a <code>customer_id<\/code> \u4f46 <code>customers<\/code> \u8868\u4e2d\u4e0d\u5b58\u5728\u8be5 <code>id<\/code>\uff0c\u5219 <code>name<\/code> \u4f1a\u662f <code>NULL<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. CROSS JOIN\uff08\u4ea4\u53c9\u8fde\u63a5\uff09<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>CROSS JOIN<\/code> \u751f\u6210\u7b1b\u5361\u5c14\u79ef\uff0c\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4e0d\u9700\u8981 <code>ON<\/code> \u6761\u4ef6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.1 \u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT A.*, B.*\nFROM table_A A\nCROSS JOIN table_B B;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.2 \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT customers.name, orders.product\nFROM customers\nCROSS JOIN orders;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c <code>customers<\/code> \u8868\u6709 3 \u6761\u8bb0\u5f55\uff0c<code>orders<\/code> \u8868\u6709 2 \u6761\u8bb0\u5f55\uff0c\u5219\u7ed3\u679c\u5c06\u6709 <code>3 \u00d7 2 = 6<\/code> \u884c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. SELF JOIN\uff08\u81ea\u8fde\u63a5\uff09<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>SELF JOIN<\/code> \u662f\u540c\u4e00\u4e2a\u8868\u7684\u8fde\u63a5\uff0c\u7528\u4e8e\u67e5\u627e\u76f8\u540c\u8868\u4e2d\u7684\u76f8\u5173\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.1 \u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT A.*, B.*\nFROM table_A A\nINNER JOIN table_A B\nON A.column = B.column;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.2 \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE employees (\n    id SERIAL PRIMARY KEY,\n    name VARCHAR(100),\n    manager_id INT\n);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u63d2\u5165\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO employees (id, name, manager_id)\nVALUES (1, 'Alice', NULL),\n       (2, 'Bob', 1),\n       (3, 'Charlie', 1),\n       (4, 'David', 2);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u8be2\u6bcf\u4e2a\u5458\u5de5\u7684\u4e0a\u7ea7\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT e1.name AS employee, e2.name AS manager\nFROM employees e1\nLEFT JOIN employees e2\nON e1.manager_id = e2.id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>employee<\/th><th>manager<\/th><\/tr><\/thead><tbody><tr><td>Alice<\/td><td>NULL<\/td><\/tr><tr><td>Bob<\/td><td>Alice<\/td><\/tr><tr><td>Charlie<\/td><td>Alice<\/td><\/tr><tr><td>David<\/td><td>Bob<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. \u4f7f\u7528 <code>USING<\/code> \u4ee3\u66ff <code>ON<\/code><\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8fde\u63a5\u7684\u5217\u540d\u76f8\u540c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>USING<\/code> \u5173\u952e\u5b57\u7b80\u5316 <code>JOIN<\/code> \u8bed\u53e5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT customers.name, orders.product\nFROM customers\nINNER JOIN orders USING (id);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u8fde\u63a5\u7c7b\u578b<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>INNER JOIN<\/code><\/td><td>\u53ea\u8fd4\u56de\u5339\u914d\u7684\u6570\u636e<\/td><\/tr><tr><td><code>LEFT JOIN<\/code><\/td><td>\u8fd4\u56de\u5de6\u8868\u6240\u6709\u6570\u636e\uff0c\u53f3\u8868\u4e0d\u5339\u914d\u8fd4\u56de NULL<\/td><\/tr><tr><td><code>RIGHT JOIN<\/code><\/td><td>\u8fd4\u56de\u53f3\u8868\u6240\u6709\u6570\u636e\uff0c\u5de6\u8868\u4e0d\u5339\u914d\u8fd4\u56de NULL<\/td><\/tr><tr><td><code>FULL JOIN<\/code><\/td><td>\u8fd4\u56de\u4e24\u8868\u6240\u6709\u6570\u636e\uff0c\u4e0d\u5339\u914d\u7684\u90e8\u5206\u4e3a NULL<\/td><\/tr><tr><td><code>CROSS JOIN<\/code><\/td><td>\u751f\u6210\u4e24\u8868\u7684\u7b1b\u5361\u5c14\u79ef<\/td><\/tr><tr><td><code>SELF JOIN<\/code><\/td><td>\u5728\u540c\u4e00\u4e2a\u8868\u5185\u67e5\u627e\u76f8\u5173\u6570\u636e<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PostgreSQL \u4e2d\uff0cJOIN \u7528\u4e8e\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u8868\u7684\u6570\u636e\u5173\u8054\u8d77\u6765\uff0c\u6839\u636e\u7279\u5b9a\u7684\u6761\u4ef6\u7ec4\u5408\u6570\u636e\u3002 1. JO [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2655,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2654","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2654"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2654\/revisions"}],"predecessor-version":[{"id":2656,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2654\/revisions\/2656"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2655"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}