{"id":5727,"date":"2024-11-14T11:57:40","date_gmt":"2024-11-14T03:57:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5727\/"},"modified":"2024-11-14T11:57:40","modified_gmt":"2024-11-14T03:57:40","slug":"sql%e4%b8%89%e8%a1%a8%e5%85%b3%e8%81%94%e6%80%8e%e4%b9%88%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5727\/","title":{"rendered":"sql\u4e09\u8868\u5173\u8054\u600e\u4e48\u67e5\u8be2"},"content":{"rendered":"<blockquote><p>\n  \u4e09\u8868\u5173\u8054\u662f\u4e00\u79cd sql \u67e5\u8be2\u6280\u672f\uff0c\u901a\u8fc7\u6307\u5b9a\u8fde\u63a5\u6761\u4ef6\u5c06\u4e09\u4e2a\u6216\u66f4\u591a\u8868\u8fde\u63a5\u8d77\u6765\u83b7\u53d6\u4fe1\u606f\u3002\u4e3b\u8981\u6709\u4e09\u79cd\u5173\u8054\u7c7b\u578b\uff1a\u5185\u8fde\u63a5\uff0c\u5de6\u5916\u8fde\u63a5\u548c\u53f3\u5916\u8fde\u63a5\u3002\u4f7f\u7528 join \u8bed\u53e5\u8fdb\u884c\u4e09\u8868\u5173\u8054\uff0c\u6307\u5b9a\u8fde\u63a5\u8868\u4e4b\u95f4\u7684\u5217\u76f8\u7b49\u4f5c\u4e3a\u8fde\u63a5\u6761\u4ef6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053021302221325.jpg\" class=\"aligncenter\" title=\"sql\u4e09\u8868\u5173\u8054\u600e\u4e48\u67e5\u8be2\u63d2\u56fe\" alt=\"sql\u4e09\u8868\u5173\u8054\u600e\u4e48\u67e5\u8be2\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e09\u8868\u5173\u8054\u67e5\u8be2<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u4e09\u8868\u5173\u8054\uff1f<\/strong><\/p>\n<p>\u4e09\u8868\u5173\u8054\u662f\u4e00\u79cd SQL \u67e5\u8be2\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u60a8\u5c06\u4e09\u4e2a\u6216\u66f4\u591a\u4e2a\u8868\u8fde\u63a5\u8d77\u6765\u4ee5\u83b7\u53d6\u4fe1\u606f\u3002\u901a\u8fc7\u6307\u5b9a\u8fde\u63a5\u6761\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4ece\u4e0d\u540c\u7684\u8868\u4e2d\u63d0\u53d6\u5e76\u7ec4\u5408\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u8fdb\u884c\u4e09\u8868\u5173\u8054\uff1f<\/strong><\/p>\n<p>\u8981\u4f7f\u7528 SQL \u8fdb\u884c\u4e09\u8868\u5173\u8054\uff0c\u8bf7\u4f7f\u7528 JOIN \u8bed\u53e5\u3002\u6709\u4e09\u79cd\u4e3b\u8981\u7c7b\u578b\u7684\u5173\u8054\uff1a<\/p>\n<ul>\n<li> <strong>\u5185\u8fde\u63a5 (INNER JOIN)\uff1a<\/strong>\u4ec5\u8fd4\u56de\u5728\u6240\u6709\u8fde\u63a5\u8868\u4e2d\u90fd\u5b58\u5728\u5339\u914d\u8bb0\u5f55\u7684\u884c\u3002<\/li>\n<li> <strong>\u5de6\u5916\u8fde\u63a5 (LEFT OUTER JOIN)\uff1a<\/strong>\u8fd4\u56de\u5de6\u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u5373\u4f7f\u5b83\u4eec\u5728\u5176\u4ed6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u9879\u3002<\/li>\n<li> <strong>\u53f3\u5916\u8fde\u63a5 (RIGHT OUTER JOIN)\uff1a<\/strong>\u8fd4\u56de\u53f3\u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u5373\u4f7f\u5b83\u4eec\u5728\u5176\u4ed6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u9879\u3002<\/li>\n<\/ul>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT *\nFROM table1\nJOIN table2 ON table1.column = table2.column\nJOIN table3 ON table2.column = table3.column;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e09\u4e2a\u8868\uff1a<\/p>\n<ul>\n<li>customers: \u5305\u542b\u5ba2\u6237\u4fe1\u606f\uff08customer_id\u3001name\u3001address)<\/li>\n<li>orders: \u5305\u542b\u8ba2\u5355\u4fe1\u606f\uff08order_id\u3001customer_id\u3001product_id)<\/li>\n<li>products: \u5305\u542b\u4ea7\u54c1\u4fe1\u606f\uff08product_id\u3001name\u3001price)<\/li>\n<\/ul>\n<p>\u8981\u67e5\u627e\u67d0\u4e2a\u5ba2\u6237\u7684\u6240\u6709\u8ba2\u5355\u53ca\u5176\u5173\u8054\u7684\u4ea7\u54c1\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT *\nFROM customers c\nINNER JOIN orders o ON c.customer_id = o.customer_id\nINNER JOIN products p ON o.product_id = p.product_id;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u67e5\u8be2\u5c06\u8fd4\u56de\u6240\u6709\u5ba2\u6237\u7684\u884c\uff0c\u4ee5\u53ca\u4ed6\u4eec\u7684\u6240\u6709\u8ba2\u5355\u548c\u6240\u8ba2\u8d2d\u7684\u4ea7\u54c1\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u4e09\u8868\u5173\u8054\u600e\u4e48\u67e5\u8be2\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e09\u8868\u5173\u8054\u662f\u4e00\u79cd sql \u67e5\u8be2\u6280\u672f\uff0c\u901a\u8fc7\u6307\u5b9a\u8fde\u63a5\u6761\u4ef6\u5c06\u4e09\u4e2a\u6216\u66f4\u591a\u8868\u8fde\u63a5\u8d77\u6765\u83b7\u53d6\u4fe1\u606f\u3002\u4e3b\u8981\u6709\u4e09\u79cd\u5173\u8054\u7c7b\u578b\uff1a\u5185\u8fde\u63a5\uff0c\u5de6\u5916\u8fde\u63a5\u548c\u53f3\u5916\u8fde\u63a5\u3002\u4f7f\u7528 join \u8bed\u53e5\u8fdb\u884c\u4e09\u8868\u5173\u8054\uff0c\u6307\u5b9a\u8fde\u63a5\u8868\u4e4b\u95f4\u7684\u5217\u76f8\u7b49\u4f5c\u4e3a\u8fde\u63a5\u6761\u4ef6\u3002 SQL \u4e09\u8868\u5173\u8054\u67e5\u8be2 \u4ec0\u4e48\u662f\u4e09\u8868\u5173\u8054\uff1f \u4e09\u8868\u5173\u8054\u662f\u4e00\u79cd SQL \u67e5\u8be2\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u60a8\u5c06\u4e09\u4e2a\u6216\u66f4\u591a\u4e2a\u8868\u8fde\u63a5\u8d77\u6765\u4ee5\u83b7\u53d6\u4fe1\u606f\u3002\u901a\u8fc7\u6307\u5b9a\u8fde\u63a5\u6761\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4ece\u4e0d\u540c\u7684\u8868\u4e2d\u63d0\u53d6\u5e76\u7ec4\u5408\u6570\u636e\u3002 \u5982\u4f55\u8fdb\u884c\u4e09\u8868\u5173\u8054\uff1f \u8981\u4f7f\u7528 SQL \u8fdb\u884c\u4e09\u8868\u5173\u8054\uff0c\u8bf7\u4f7f\u7528 JOIN \u8bed\u53e5\u3002\u6709\u4e09\u79cd\u4e3b\u8981\u7c7b\u578b\u7684\u5173\u8054\uff1a \u5185\u8fde\u63a5 (INNER JOIN)\uff1a\u4ec5\u8fd4\u56de\u5728\u6240\u6709\u8fde\u63a5\u8868\u4e2d\u90fd\u5b58\u5728\u5339\u914d\u8bb0\u5f55\u7684\u884c\u3002 \u5de6\u5916\u8fde\u63a5 (LEFT OUTER JOIN)\uff1a\u8fd4\u56de\u5de6\u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u5373\u4f7f\u5b83\u4eec\u5728\u5176\u4ed6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u9879\u3002 \u53f3\u5916\u8fde\u63a5 (RIGHT OUTER JOIN)\uff1a\u8fd4\u56de\u53f3\u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u5373\u4f7f\u5b83\u4eec\u5728\u5176\u4ed6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u9879\u3002 \u8bed\u6cd5\uff1a SELECT * FROM table1 JOIN table2 ON table1.column = table2.column JOIN table3 ON table2.column = table3.column; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a \u5047\u8bbe\u6211\u4eec\u6709\u4e09\u4e2a\u8868\uff1a customers: \u5305\u542b\u5ba2\u6237\u4fe1\u606f\uff08customer_id\u3001name\u3001address) orders: \u5305\u542b\u8ba2\u5355\u4fe1\u606f\uff08order_id\u3001customer_id\u3001product_id) products: \u5305\u542b\u4ea7\u54c1\u4fe1\u606f\uff08product_id\u3001name\u3001price) \u8981\u67e5\u627e\u67d0\u4e2a\u5ba2\u6237\u7684\u6240\u6709\u8ba2\u5355\u53ca\u5176\u5173\u8054\u7684\u4ea7\u54c1\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5727","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5727"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5727\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}