{"id":5229,"date":"2024-11-14T08:13:02","date_gmt":"2024-11-14T00:13:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5229\/"},"modified":"2024-11-14T08:13:02","modified_gmt":"2024-11-14T00:13:02","slug":"mysql%e8%bf%9e%e8%a1%a8%e6%9f%a5%e8%af%a2%e6%80%8e%e4%b9%88%e6%9f%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5229\/","title":{"rendered":"mysql\u8fde\u8868\u67e5\u8be2\u600e\u4e48\u67e5"},"content":{"rendered":"<blockquote><p>\n  \u8fde\u8868\u67e5\u8be2\u662f\u5c06\u591a\u8868\u5173\u8054\u4ee5\u5408\u5e76\u5b57\u6bb5\u83b7\u53d6\u591a\u8868\u6570\u636e\u3002mysql\u4e2d\u4f7f\u7528 join \u5173\u952e\u5b57\u8fdb\u884c\u8fde\u8868\u67e5\u8be2\uff0c\u5e38\u89c1\u7c7b\u578b\u6709\uff1a\u5185\u8fde\u63a5\u3001\u5de6\u8fde\u63a5\u3001\u53f3\u8fde\u63a5\u3001\u5168\u8fde\u63a5\u3002\u8fde\u8868\u67e5\u8be2\u8bed\u6cd5\u4e3a\uff1aselect * from table1 join table2 on table1.column = table2.column\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/26\/2024052617040725632.jpg\" class=\"aligncenter\" title=\"mysql\u8fde\u8868\u67e5\u8be2\u600e\u4e48\u67e5\u63d2\u56fe\" alt=\"mysql\u8fde\u8868\u67e5\u8be2\u600e\u4e48\u67e5\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u8fde\u8868\u67e5\u8be2<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u8fde\u8868\u67e5\u8be2\uff1f<\/strong><\/p>\n<p>\u8fde\u8868\u67e5\u8be2\u662f\u5728 MySQL \u4e2d\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u8868\u5173\u8054\u8d77\u6765\uff0c\u5408\u5e76\u8fd9\u4e9b\u8868\u7684\u5b57\u6bb5\uff0c\u83b7\u53d6\u591a\u8868\u6570\u636e\u7684\u65b9\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u8fdb\u884c\u8fde\u8868\u67e5\u8be2\uff1f<\/strong><\/p>\n<p>MySQL \u4e2d\u4f7f\u7528 JOIN \u5173\u952e\u5b57\u8fdb\u884c\u8fde\u8868\u67e5\u8be2\u3002JOIN \u5173\u952e\u5b57\u7528\u4e8e\u6307\u5b9a\u8981\u5173\u8054\u7684\u8868\u53ca\u5176\u5173\u8054\u6761\u4ef6\u3002<\/p>\n<p><strong>\u8fde\u8868\u67e5\u8be2\u7684\u7c7b\u578b<\/strong><\/p>\n<p>\u6709\u56db\u79cd\u4e3b\u8981\u7684\u8fde\u8868\u67e5\u8be2\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li> <strong>\u5185\u8fde\u63a5 (INNER JOIN)<\/strong>\uff1a\u53ea\u8fd4\u56de\u540c\u65f6\u5b58\u5728\u4e8e\u4e24\u4e2a\u8868\u4e2d\u7684\u5339\u914d\u884c\u3002<\/li>\n<li> <strong>\u5de6\u8fde\u63a5 (LEFT JOIN)<\/strong>\uff1a\u8fd4\u56de\u5de6\u8868\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5b83\u4eec\u5728\u53f3\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u9879\u3002<\/li>\n<li> <strong>\u53f3\u8fde\u63a5 (RIGHT JOIN)<\/strong>\uff1a\u8fd4\u56de\u53f3\u8868\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5b83\u4eec\u5728\u5de6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u9879\u3002<\/li>\n<li> <strong>\u5168\u8fde\u63a5 (FULL JOIN)<\/strong>\uff1a\u8fd4\u56de\u4e24\u4e2a\u8868\u7684\u6240\u6709\u884c\uff0c\u65e0\u8bba\u662f\u5426\u5b58\u5728\u5339\u914d\u9879\u3002<\/li>\n<\/ul>\n<p><strong>\u8fde\u8868\u67e5\u8be2\u8bed\u6cd5<\/strong><\/p>\n<pre>SELECT *\nFROM table1\nJOIN table2 ON table1.column = table2.column;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>* \u8868\u793a\u8981\u9009\u62e9\u7684\u5b57\u6bb5\u3002<\/li>\n<li>table1 \u548c table2 \u662f\u8981\u5173\u8054\u7684\u8868\u3002<\/li>\n<li>table1.column \u548c table2.column \u662f\u5173\u8054\u6761\u4ef6\uff0c\u5373\u8fd9\u4e24\u4e2a\u8868\u7684\u5171\u540c\u5b57\u6bb5\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u67e5\u8be2\u5c06 customers \u8868\u548c orders \u8868\u8fde\u63a5\u8d77\u6765\uff0c\u6839\u636e customer_id \u5173\u8054\u8fd9\u4e24\u4e2a\u8868\uff0c\u5e76\u8fd4\u56de\u4e24\u4e2a\u8868\u4e2d\u7684\u6240\u6709\u5b57\u6bb5\uff1a<\/p>\n<pre>SELECT *\nFROM customers\nINNER JOIN orders ON customers.customer_id = orders.customer_id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u8fde\u8868\u67e5\u8be2\u600e\u4e48\u67e5\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>\u8fde\u8868\u67e5\u8be2\u662f\u5c06\u591a\u8868\u5173\u8054\u4ee5\u5408\u5e76\u5b57\u6bb5\u83b7\u53d6\u591a\u8868\u6570\u636e\u3002mysql\u4e2d\u4f7f\u7528 join \u5173\u952e\u5b57\u8fdb\u884c\u8fde\u8868\u67e5\u8be2\uff0c\u5e38\u89c1\u7c7b\u578b\u6709\uff1a\u5185\u8fde\u63a5\u3001\u5de6\u8fde\u63a5\u3001\u53f3\u8fde\u63a5\u3001\u5168\u8fde\u63a5\u3002\u8fde\u8868\u67e5\u8be2\u8bed\u6cd5\u4e3a\uff1aselect * from table1 join table2 on table1.column = table2.column\u3002 MySQL \u8fde\u8868\u67e5\u8be2 \u4ec0\u4e48\u662f\u8fde\u8868\u67e5\u8be2\uff1f \u8fde\u8868\u67e5\u8be2\u662f\u5728 MySQL \u4e2d\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u8868\u5173\u8054\u8d77\u6765\uff0c\u5408\u5e76\u8fd9\u4e9b\u8868\u7684\u5b57\u6bb5\uff0c\u83b7\u53d6\u591a\u8868\u6570\u636e\u7684\u65b9\u5f0f\u3002 \u5982\u4f55\u8fdb\u884c\u8fde\u8868\u67e5\u8be2\uff1f MySQL \u4e2d\u4f7f\u7528 JOIN \u5173\u952e\u5b57\u8fdb\u884c\u8fde\u8868\u67e5\u8be2\u3002JOIN \u5173\u952e\u5b57\u7528\u4e8e\u6307\u5b9a\u8981\u5173\u8054\u7684\u8868\u53ca\u5176\u5173\u8054\u6761\u4ef6\u3002 \u8fde\u8868\u67e5\u8be2\u7684\u7c7b\u578b \u6709\u56db\u79cd\u4e3b\u8981\u7684\u8fde\u8868\u67e5\u8be2\u7c7b\u578b\uff1a \u5185\u8fde\u63a5 (INNER JOIN)\uff1a\u53ea\u8fd4\u56de\u540c\u65f6\u5b58\u5728\u4e8e\u4e24\u4e2a\u8868\u4e2d\u7684\u5339\u914d\u884c\u3002 \u5de6\u8fde\u63a5 (LEFT JOIN)\uff1a\u8fd4\u56de\u5de6\u8868\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5b83\u4eec\u5728\u53f3\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u9879\u3002 \u53f3\u8fde\u63a5 (RIGHT JOIN)\uff1a\u8fd4\u56de\u53f3\u8868\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5b83\u4eec\u5728\u5de6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u9879\u3002 \u5168\u8fde\u63a5 (FULL JOIN)\uff1a\u8fd4\u56de\u4e24\u4e2a\u8868\u7684\u6240\u6709\u884c\uff0c\u65e0\u8bba\u662f\u5426\u5b58\u5728\u5339\u914d\u9879\u3002 \u8fde\u8868\u67e5\u8be2\u8bed\u6cd5 SELECT * FROM table1 JOIN table2 ON table1.column = table2.column; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a * \u8868\u793a\u8981\u9009\u62e9\u7684\u5b57\u6bb5\u3002 table1 \u548c [&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-5229","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5229","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=5229"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5229\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}