{"id":5217,"date":"2024-11-14T12:46:55","date_gmt":"2024-11-14T04:46:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5217\/"},"modified":"2024-11-14T12:46:55","modified_gmt":"2024-11-14T04:46:55","slug":"sql%e5%a4%9a%e8%a1%a8%e5%85%b3%e8%81%94%e6%9f%a5%e8%af%a2%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5217\/","title":{"rendered":"sql\u591a\u8868\u5173\u8054\u67e5\u8be2\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  sql \u591a\u8868\u5173\u8054\u67e5\u8be2\u901a\u8fc7\u4f7f\u7528 join \u5173\u952e\u5b57\u7ec4\u5408\u4e0d\u540c\u8868\u4e2d\u7684\u8bb0\u5f55\u3002\u4e3b\u8981\u5173\u8054\u7c7b\u578b\u5305\u62ec\u5185\u8fde\u63a5\u3001\u5de6\u5916\u8fde\u63a5\u3001\u53f3\u5916\u8fde\u63a5\u548c\u5168\u5916\u8fde\u63a5\uff0c\u901a\u8fc7\u6307\u5b9a\u57fa\u4e8e\u5171\u540c\u5217\u503c\u7684\u5173\u8054\u6761\u4ef6\u5b9e\u73b0\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/24\/2024052421304524884.jpg\" class=\"aligncenter\" title=\"sql\u591a\u8868\u5173\u8054\u67e5\u8be2\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u591a\u8868\u5173\u8054\u67e5\u8be2\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u591a\u8868\u5173\u8054\u67e5\u8be2\u7528\u6cd5<\/strong><\/p>\n<p>\u591a\u8868\u5173\u8054\u67e5\u8be2\u662f\u5c06\u6765\u81ea\u4e0d\u540c\u8868\u7684\u76f8\u5173\u8bb0\u5f55\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u67e5\u8be2\u6280\u672f\u3002\u5728 SQL \u4e2d\uff0c\u5173\u8054\u67e5\u8be2\u901a\u8fc7\u4f7f\u7528 JOIN \u5173\u952e\u5b57\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u786e\u5b9a\u9700\u8981\u5173\u8054\u7684\u8868\uff1a<\/strong>\u8bc6\u522b\u9700\u8981\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u6570\u636e\u6240\u5728\u7684\u8868\u3002<\/li>\n<li> <strong>\u9009\u62e9\u5173\u8054\u7c7b\u578b\uff1a<\/strong>\u6709\u56db\u79cd\u4e3b\u8981\u7684\u5173\u8054\u7c7b\u578b\uff1a\u5185\u8fde\u63a5\u3001\u5de6\u5916\u8fde\u63a5\u3001\u53f3\u5916\u8fde\u63a5\u548c\u5168\u5916\u8fde\u63a5\u3002\u9009\u62e9\u6700\u80fd\u8868\u793a\u6240\u9700\u5173\u7cfb\u7684\u5173\u8054\u7c7b\u578b\u3002<\/li>\n<li> <strong>\u6307\u5b9a\u5173\u8054\u6761\u4ef6\uff1a<\/strong>\u4f7f\u7528 ON \u5173\u952e\u5b57\u6307\u5b9a\u5c06\u8868\u5173\u8054\u5728\u4e00\u8d77\u7684\u6761\u4ef6\u3002\u5173\u8054\u6761\u4ef6\u901a\u5e38\u57fa\u4e8e\u5171\u540c\u7684\u5217\u503c\u3002<\/li>\n<li> <strong>\u7f16\u5199\u67e5\u8be2\uff1a<\/strong>\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u7f16\u5199\u67e5\u8be2\uff1a<\/li>\n<\/ol>\n<pre>SELECT *\nFROM table1\nJOIN table2 ON table1.column1 = table2.column2<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5173\u8054\u7c7b\u578b\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u5185\u8fde\u63a5\uff1a<\/strong>\u53ea\u8fd4\u56de\u5177\u6709\u5339\u914d\u884c\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>\u5de6\u5916\u8fde\u63a5\uff1a<\/strong>\u4ece\u5de6\u8868\u8fd4\u56de\u6240\u6709\u884c\uff0c\u5373\u4f7f\u53f3\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u884c\u3002<\/li>\n<li> <strong>\u53f3\u5916\u8fde\u63a5\uff1a<\/strong>\u4ece\u53f3\u8868\u8fd4\u56de\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5de6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u884c\u3002<\/li>\n<li> <strong>\u5168\u5916\u8fde\u63a5\uff1a<\/strong>\u4ece\u5de6\u8868\u548c\u53f3\u8868\u8fd4\u56de\u6240\u6709\u884c\uff0c\u5373\u4f7f\u6ca1\u6709\u5339\u914d\u7684\u884c\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u60a8\u6709\u4ee5\u4e0b\u4e24\u4e2a\u8868\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u8ba2\u5355\u8868\uff08Orders\uff09\uff1a<\/strong><\/p>\n<pre>+----+------+-----+\n| Id | Date | CustomerId |\n+----+------+-----+\n| 1   | 2023-03-08 | 10 |\n| 2   | 2023-03-10 | 20 |\n| 3   | 2023-03-12 | 30 |\n+----+------+-----+<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u5ba2\u6237\u8868\uff08Customers\uff09\uff1a<\/strong><\/p>\n<pre>+----+--------+--------+\n| Id | Name | Address |\n+----+--------+--------+\n| 10  | Alice  | 123 Main St |\n| 20  | Bob    | 456 Elm St |\n| 30  | Charlie | 789 Oak St |\n+----+--------+--------+<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<\/ul>\n<p><strong>\u5173\u8054\u67e5\u8be2\uff1a<\/strong><\/p>\n<pre>SELECT *\nFROM Orders o\nJOIN Customers c ON o.CustomerId = c.Id<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre>+----+------+-----+----+--------+--------+\n| Id | Date | CustomerId | Id | Name | Address |\n+----+------+-----+----+--------+--------+\n| 1   | 2023-03-08 | 10 | 10  | Alice  | 123 Main St |\n| 2   | 2023-03-10 | 20 | 20  | Bob    | 456 Elm St |\n| 3   | 2023-03-12 | 30 | 30  | Charlie | 789 Oak St |\n+----+------+-----+----+--------+--------+<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u67e5\u8be2\u7ec4\u5408\u4e86\u4e24\u4e2a\u8868\u4e2d\u7684\u6570\u636e\uff0c\u4ee5\u83b7\u53d6\u6bcf\u4e2a\u8ba2\u5355\u53ca\u5176\u76f8\u5173\u5ba2\u6237\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u591a\u8868\u5173\u8054\u67e5\u8be2\u600e\u4e48\u7528\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>sql \u591a\u8868\u5173\u8054\u67e5\u8be2\u901a\u8fc7\u4f7f\u7528 join \u5173\u952e\u5b57\u7ec4\u5408\u4e0d\u540c\u8868\u4e2d\u7684\u8bb0\u5f55\u3002\u4e3b\u8981\u5173\u8054\u7c7b\u578b\u5305\u62ec\u5185\u8fde\u63a5\u3001\u5de6\u5916\u8fde\u63a5\u3001\u53f3\u5916\u8fde\u63a5\u548c\u5168\u5916\u8fde\u63a5\uff0c\u901a\u8fc7\u6307\u5b9a\u57fa\u4e8e\u5171\u540c\u5217\u503c\u7684\u5173\u8054\u6761\u4ef6\u5b9e\u73b0\u3002 SQL \u591a\u8868\u5173\u8054\u67e5\u8be2\u7528\u6cd5 \u591a\u8868\u5173\u8054\u67e5\u8be2\u662f\u5c06\u6765\u81ea\u4e0d\u540c\u8868\u7684\u76f8\u5173\u8bb0\u5f55\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u67e5\u8be2\u6280\u672f\u3002\u5728 SQL \u4e2d\uff0c\u5173\u8054\u67e5\u8be2\u901a\u8fc7\u4f7f\u7528 JOIN \u5173\u952e\u5b57\u5b9e\u73b0\u3002 \u4f7f\u7528\u65b9\u6cd5\uff1a \u786e\u5b9a\u9700\u8981\u5173\u8054\u7684\u8868\uff1a\u8bc6\u522b\u9700\u8981\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u6570\u636e\u6240\u5728\u7684\u8868\u3002 \u9009\u62e9\u5173\u8054\u7c7b\u578b\uff1a\u6709\u56db\u79cd\u4e3b\u8981\u7684\u5173\u8054\u7c7b\u578b\uff1a\u5185\u8fde\u63a5\u3001\u5de6\u5916\u8fde\u63a5\u3001\u53f3\u5916\u8fde\u63a5\u548c\u5168\u5916\u8fde\u63a5\u3002\u9009\u62e9\u6700\u80fd\u8868\u793a\u6240\u9700\u5173\u7cfb\u7684\u5173\u8054\u7c7b\u578b\u3002 \u6307\u5b9a\u5173\u8054\u6761\u4ef6\uff1a\u4f7f\u7528 ON \u5173\u952e\u5b57\u6307\u5b9a\u5c06\u8868\u5173\u8054\u5728\u4e00\u8d77\u7684\u6761\u4ef6\u3002\u5173\u8054\u6761\u4ef6\u901a\u5e38\u57fa\u4e8e\u5171\u540c\u7684\u5217\u503c\u3002 \u7f16\u5199\u67e5\u8be2\uff1a\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u7f16\u5199\u67e5\u8be2\uff1a SELECT * FROM table1 JOIN table2 ON table1.column1 = table2.column2 \u767b\u5f55\u540e\u590d\u5236 \u5173\u8054\u7c7b\u578b\uff1a \u5185\u8fde\u63a5\uff1a\u53ea\u8fd4\u56de\u5177\u6709\u5339\u914d\u884c\u7684\u8bb0\u5f55\u3002 \u5de6\u5916\u8fde\u63a5\uff1a\u4ece\u5de6\u8868\u8fd4\u56de\u6240\u6709\u884c\uff0c\u5373\u4f7f\u53f3\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u884c\u3002 \u53f3\u5916\u8fde\u63a5\uff1a\u4ece\u53f3\u8868\u8fd4\u56de\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5de6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u884c\u3002 \u5168\u5916\u8fde\u63a5\uff1a\u4ece\u5de6\u8868\u548c\u53f3\u8868\u8fd4\u56de\u6240\u6709\u884c\uff0c\u5373\u4f7f\u6ca1\u6709\u5339\u914d\u7684\u884c\u3002 \u793a\u4f8b\uff1a \u5047\u8bbe\u60a8\u6709\u4ee5\u4e0b\u4e24\u4e2a\u8868\uff1a \u8ba2\u5355\u8868\uff08Orders\uff09\uff1a +&#8212;-+&#8212;&#8212;+&#8212;&#8211;+ | Id | Date | CustomerId | +&#8212;-+&#8212;&#8212;+&#8212;&#8211;+ | 1 | 2023-03-08 | 10 | | 2 [&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-5217","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5217","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=5217"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5217\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}