{"id":143749,"date":"2024-11-01T00:55:26","date_gmt":"2024-10-31T16:55:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/143749\/"},"modified":"2024-11-01T00:55:27","modified_gmt":"2024-10-31T16:55:27","slug":"sql-%e6%9f%a5%e8%a9%a2%e5%a4%9a%e5%80%8b%e8%a1%a8%e6%a0%bc%ef%bc%8c%e5%a6%82%e4%bd%95%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f%ef%bc%88sql-%e5%a6%82%e4%bd%95%e6%9f%a5%e8%a9%a2%e5%a4%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/143749\/","title":{"rendered":"SQL \u67e5\u8a62\u591a\u500b\u8868\u683c\uff0c\u5982\u4f55\u67e5\u8a62\u6578\u64da\u5eab\uff1f\uff08sql \u5982\u4f55\u67e5\u8a62\u591a\u500b\u8868\u683c\u7684\u6578\u64da\u5eab\uff09"},"content":{"rendered":"<h1 id=\"sql-%e6%9f%a5%e8%a9%a2%e5%a4%9a%e5%80%8b%e8%a1%a8%e6%a0%bc%ef%bc%8c%e5%a6%82%e4%bd%95%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-IIkKWGOvzz\">SQL \u67e5\u8a62\u591a\u500b\u8868\u683c\uff0c\u5982\u4f55\u67e5\u8a62\u6578\u64da\u5eab\uff1f<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0cSQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u5f9e\u591a\u500b\u8868\u683c\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6578\u64da\u3002\u7576\u6578\u64da\u5206\u6563\u5728\u4e0d\u540c\u7684\u8868\u683c\u4e2d\u6642\uff0c\u5b78\u6703\u5982\u4f55\u6709\u6548\u5730\u67e5\u8a62\u9019\u4e9b\u8868\u683c\u8b8a\u5f97\u5c24\u70ba\u91cd\u8981\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 SQL \u67e5\u8a62\u591a\u500b\u8868\u683c\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u6280\u5de7\u3002<\/p>\n<h2 id=\"%e7%90%86%e8%a7%a3%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e7%b5%90%e6%a7%8b-IIkKWGOvzz\">\u7406\u89e3\u6578\u64da\u5eab\u7684\u7d50\u69cb<\/h2>\n<p>\u5728\u958b\u59cb\u67e5\u8a62\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u4e86\u89e3\u6578\u64da\u5eab\u7684\u7d50\u69cb\u3002\u6578\u64da\u5eab\u901a\u5e38\u7531\u591a\u500b\u8868\u683c\u7d44\u6210\uff0c\u6bcf\u500b\u8868\u683c\u5305\u542b\u4e0d\u540c\u7684\u6578\u64da\u985e\u578b\u3002\u8868\u683c\u4e4b\u9593\u53ef\u80fd\u5b58\u5728\u95dc\u806f\uff0c\u4f8b\u5982\u5916\u9375\uff08Foreign Key\uff09\u95dc\u806f\uff0c\u9019\u4f7f\u5f97\u5f9e\u591a\u500b\u8868\u683c\u4e2d\u67e5\u8a62\u6578\u64da\u6210\u70ba\u53ef\u80fd\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8-join-%e8%aa%9e%e5%8f%a5%e6%9f%a5%e8%a9%a2%e5%a4%9a%e5%80%8b%e8%a1%a8%e6%a0%bc-IIkKWGOvzz\">\u4f7f\u7528 JOIN \u8a9e\u53e5\u67e5\u8a62\u591a\u500b\u8868\u683c<\/h2>\n<p>\u5728 SQL \u4e2d\uff0c\u6700\u5e38\u7528\u7684\u67e5\u8a62\u591a\u500b\u8868\u683c\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 JOIN \u8a9e\u53e5\u3002JOIN \u8a9e\u53e5\u5141\u8a31\u7528\u6236\u6839\u64da\u67d0\u4e9b\u689d\u4ef6\u5c07\u5169\u500b\u6216\u591a\u500b\u8868\u683c\u7684\u6578\u64da\u7d50\u5408\u5728\u4e00\u8d77\u3002\u4ee5\u4e0b\u662f\u5e7e\u7a2e\u5e38\u898b\u7684 JOIN \u985e\u578b\uff1a<\/p>\n<ul>\n<li><strong>INNER JOIN<\/strong>\uff1a\u53ea\u8fd4\u56de\u5169\u500b\u8868\u683c\u4e2d\u5339\u914d\u7684\u884c\u3002<\/li>\n<li><strong>LEFT JOIN<\/strong>\uff1a\u8fd4\u56de\u5de6\u5074\u8868\u683c\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u53f3\u5074\u8868\u683c\u4e2d\u6c92\u6709\u5339\u914d\u7684\u884c\u3002<\/li>\n<li><strong>RIGHT JOIN<\/strong>\uff1a\u8fd4\u56de\u53f3\u5074\u8868\u683c\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5de6\u5074\u8868\u683c\u4e2d\u6c92\u6709\u5339\u914d\u7684\u884c\u3002<\/li>\n<li><strong>FULL OUTER JOIN<\/strong>\uff1a\u8fd4\u56de\u5169\u500b\u8868\u683c\u4e2d\u7684\u6240\u6709\u884c\uff0c\u7121\u8ad6\u662f\u5426\u6709\u5339\u914d\u3002<\/li>\n<\/ul>\n<h3 id=\"inner-join-%e7%a4%ba%e4%be%8b-IIkKWGOvzz\">INNER JOIN \u793a\u4f8b<\/h3>\n<pre><code>SELECT a.column1, b.column2\nFROM table1 a\nINNER JOIN table2 b ON a.common_column = b.common_column;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5f9e <code>table1<\/code> \u548c <code>table2<\/code> \u4e2d\u9078\u64c7\u4e86 <code>column1<\/code> \u548c <code>column2<\/code>\uff0c\u4e26\u6839\u64da <code>common_column<\/code> \u9032\u884c\u4e86\u5339\u914d\u3002<\/p>\n<h3 id=\"left-join-%e7%a4%ba%e4%be%8b-IIkKWGOvzz\">LEFT JOIN \u793a\u4f8b<\/h3>\n<pre><code>SELECT a.column1, b.column2\nFROM table1 a\nLEFT JOIN table2 b ON a.common_column = b.common_column;<\/code><\/pre>\n<p>\u9019\u500b\u67e5\u8a62\u5c07\u8fd4\u56de <code>table1<\/code> \u4e2d\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5728 <code>table2<\/code> \u4e2d\u6c92\u6709\u5339\u914d\u7684\u884c\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e5%ad%90%e6%9f%a5%e8%a9%a2%e6%9f%a5%e8%a9%a2%e5%a4%9a%e5%80%8b%e8%a1%a8%e6%a0%bc-IIkKWGOvzz\">\u4f7f\u7528\u5b50\u67e5\u8a62\u67e5\u8a62\u591a\u500b\u8868\u683c<\/h2>\n<p>\u9664\u4e86 JOIN \u8a9e\u53e5\uff0c\u9084\u53ef\u4ee5\u4f7f\u7528\u5b50\u67e5\u8a62\u4f86\u67e5\u8a62\u591a\u500b\u8868\u683c\u3002\u5b50\u67e5\u8a62\u662f\u5d4c\u5957\u5728\u5176\u4ed6\u67e5\u8a62\u4e2d\u7684\u67e5\u8a62\uff0c\u901a\u5e38\u7528\u65bc\u5f9e\u4e00\u500b\u8868\u683c\u4e2d\u7372\u53d6\u6578\u64da\uff0c\u7136\u5f8c\u5728\u4e3b\u67e5\u8a62\u4e2d\u4f7f\u7528\u9019\u4e9b\u6578\u64da\u3002<\/p>\n<h3 id=\"%e5%ad%90%e6%9f%a5%e8%a9%a2%e7%a4%ba%e4%be%8b-IIkKWGOvzz\">\u5b50\u67e5\u8a62\u793a\u4f8b<\/h3>\n<pre><code>SELECT column1\nFROM table1\nWHERE column2 IN (SELECT column2 FROM table2 WHERE condition);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5f9e <code>table1<\/code> \u4e2d\u9078\u64c7\u4e86 <code>column1<\/code>\uff0c\u689d\u4ef6\u662f <code>column2<\/code> \u7684\u503c\u5fc5\u9808\u5728 <code>table2<\/code> \u7684\u5b50\u67e5\u8a62\u7d50\u679c\u4e2d\u3002<\/p>\n<h2 id=\"%e7%b5%90%e5%90%88-join-%e5%92%8c%e5%ad%90%e6%9f%a5%e8%a9%a2-IIkKWGOvzz\">\u7d50\u5408 JOIN \u548c\u5b50\u67e5\u8a62<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u7d50\u5408 JOIN \u548c\u5b50\u67e5\u8a62\u53ef\u4ee5\u66f4\u9748\u6d3b\u5730\u67e5\u8a62\u6578\u64da\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5148\u4f7f\u7528\u5b50\u67e5\u8a62\u7372\u53d6\u4e00\u7d44\u6578\u64da\uff0c\u7136\u5f8c\u518d\u5c07\u5176\u8207\u5176\u4ed6\u8868\u683c\u9032\u884c JOIN\u3002<\/p>\n<pre><code>SELECT a.column1, b.column2\nFROM table1 a\nINNER JOIN (SELECT column2 FROM table2 WHERE condition) b ON a.common_column = b.column2;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-IIkKWGOvzz\">\u7e3d\u7d50<\/h2>\n<p>\u67e5\u8a62\u591a\u500b\u8868\u683c\u662f SQL \u4e2d\u4e00\u9805\u91cd\u8981\u7684\u6280\u80fd\uff0c\u7121\u8ad6\u662f\u4f7f\u7528 JOIN \u9084\u662f\u5b50\u67e5\u8a62\uff0c\u4e86\u89e3\u5982\u4f55\u6709\u6548\u5730\u5f9e\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6578\u64da\u90fd\u81f3\u95dc\u91cd\u8981\u3002\u900f\u904e\u9019\u4e9b\u6280\u8853\uff0c\u60a8\u53ef\u4ee5\u66f4\u9748\u6d3b\u5730\u8655\u7406\u548c\u5206\u6790\u6578\u64da\uff0c\u5f9e\u800c\u505a\u51fa\u66f4\u660e\u667a\u7684\u6c7a\u7b56\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u9700\u6c42\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u548c\u67e5\u8a62\u6578\u64da\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 SQL \u67e5\u8a62\u591a\u500b\u8868\u683c\uff0c\u638c\u63e1\u6578\u64da\u5eab\u67e5\u8a62\u6280\u5de7\uff0c\u63d0\u5347\u6578\u64da\u5206\u6790\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-143749","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/143749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=143749"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/143749\/revisions"}],"predecessor-version":[{"id":143750,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/143749\/revisions\/143750"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=143749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=143749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=143749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}