{"id":5154,"date":"2024-11-14T10:03:18","date_gmt":"2024-11-14T02:03:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5154\/"},"modified":"2024-11-14T10:03:18","modified_gmt":"2024-11-14T02:03:18","slug":"mysql%e6%80%8e%e4%b9%88%e5%81%9a%e8%bf%9e%e8%a1%a8%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5154\/","title":{"rendered":"mysql\u600e\u4e48\u505a\u8fde\u8868\u67e5\u8be2"},"content":{"rendered":"<blockquote><p>\n  mysql \u4f7f\u7528 inner join \u4ece\u591a\u4e2a\u8868\u4e2d\u5173\u8054\u6570\u636e\uff0c\u901a\u8fc7\u6bd4\u8f83\u8868\u4e2d\u516c\u7528\u5217\u8fdb\u884c\u5173\u8054\u3002\u8bed\u6cd5\uff1aselect \u52171, \u52172, &#8230;from \u88681 inner join \u88682 on \u88681.\u516c\u7528\u5217 = \u88682.\u516c\u7528\u5217\uff1b\u793a\u4f8b\uff1aselect students.name as student_name, courses.name as course_name from students inner join courses on students.student_id = courses.student_i\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/22\/2024052217303276218.jpg\" class=\"aligncenter\" title=\"mysql\u600e\u4e48\u505a\u8fde\u8868\u67e5\u8be2\u63d2\u56fe\" alt=\"mysql\u600e\u4e48\u505a\u8fde\u8868\u67e5\u8be2\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u8fdb\u884c\u8054\u8868\u67e5\u8be2<\/strong><\/p>\n<p>MySQL \u4e2d\u7684\u8054\u8868\u67e5\u8be2\u662f\u4e00\u79cd\u4ece\u591a\u4e2a\u8868\u4e2d\u83b7\u53d6\u6570\u636e\u7684\u6280\u672f\uff0c\u901a\u8fc7\u6bd4\u8f83\u8868\u4e2d\u7684\u516c\u7528\u5217\u6765\u5173\u8054\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u6267\u884c\u8054\u8868\u67e5\u8be2<\/strong><\/p>\n<p>\u8054\u8868\u67e5\u8be2\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT \u52171, \u52172, ...\nFROM \u88681\nINNER JOIN \u88682 ON \u88681.\u516c\u7528\u5217 = \u88682.\u516c\u7528\u5217<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>SELECT \u8bed\u53e5\u6307\u5b9a\u8981\u4ece\u8868\u4e2d\u8fd4\u56de\u7684\u5217\u3002<\/li>\n<li>FROM \u8bed\u53e5\u6307\u5b9a\u8981\u8fde\u63a5\u7684\u8868\u3002<\/li>\n<li>INNER JOIN \u8bed\u53e5\u6307\u5b9a\u8fde\u63a5\u8868\u7684\u65b9\u5f0f\u3002<\/li>\n<li>ON \u8bed\u53e5\u6307\u5b9a\u6bd4\u8f83\u8868\u4e2d\u516c\u7528\u5217\u7684\u6761\u4ef6\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff0c\u5b66\u751f \u8868\u548c \u8bfe\u7a0b \u8868\uff0c\u5b83\u4eec\u901a\u8fc7 \u5b66\u751f ID \u5217\u76f8\u5173\u8054\u3002\u8981\u83b7\u53d6\u6240\u6709\u5b66\u751f\u53ca\u5176\u53c2\u52a0\u7684\u8bfe\u7a0b\u7684\u540d\u79f0\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u8054\u8868\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT students.name AS student_name, courses.name AS course_name\nFROM students\nINNER JOIN courses ON students.student_id = courses.student_id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u8054\u8868\u67e5\u8be2\u7c7b\u578b<\/strong><\/p>\n<p>\u9664\u4e86 INNER JOIN \u4e4b\u5916\uff0cMySQL \u8fd8\u652f\u6301\u4ee5\u4e0b\u7c7b\u578b\u7684\u8054\u8868\u67e5\u8be2\uff1a<\/p>\n<ul>\n<li> <strong>LEFT JOIN\uff1a<\/strong>\u8fd4\u56de\u5de6\u8868\u7684\u5168\u90e8\u8bb0\u5f55\uff0c\u4ee5\u53ca\u4e0e\u53f3\u8868\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>RIGHT JOIN\uff1a<\/strong>\u8fd4\u56de\u53f3\u8868\u7684\u5168\u90e8\u8bb0\u5f55\uff0c\u4ee5\u53ca\u4e0e\u5de6\u8868\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/li>\n<li> <strong>FULL JOIN\uff1a<\/strong>\u8fd4\u56de\u5de6\u8868\u548c\u53f3\u8868\u7684\u6240\u6709\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<p><strong>\u63d0\u793a<\/strong><\/p>\n<ul>\n<li>\u59cb\u7ec8\u6307\u5b9a\u4e00\u4e2a\u516c\u7528\u5217\u6765\u5173\u8054\u8868\u3002<\/li>\n<li>\u4f7f\u7528\u522b\u540d\u6765\u533a\u5206\u6765\u81ea\u4e0d\u540c\u8868\u7684\u5217\u3002<\/li>\n<li>\u6839\u636e\u9700\u8981\u4f7f\u7528 WHERE \u5b50\u53e5\u8fc7\u6ee4\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u505a\u8fde\u8868\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>mysql \u4f7f\u7528 inner join \u4ece\u591a\u4e2a\u8868\u4e2d\u5173\u8054\u6570\u636e\uff0c\u901a\u8fc7\u6bd4\u8f83\u8868\u4e2d\u516c\u7528\u5217\u8fdb\u884c\u5173\u8054\u3002\u8bed\u6cd5\uff1aselect \u52171, \u52172, &#8230;from \u88681 inner join \u88682 on \u88681.\u516c\u7528\u5217 = \u88682.\u516c\u7528\u5217\uff1b\u793a\u4f8b\uff1aselect students.name as student_name, courses.name as course_name from students inner join courses on students.student_id = courses.student_i \u5982\u4f55\u4f7f\u7528 MySQL \u8fdb\u884c\u8054\u8868\u67e5\u8be2 MySQL \u4e2d\u7684\u8054\u8868\u67e5\u8be2\u662f\u4e00\u79cd\u4ece\u591a\u4e2a\u8868\u4e2d\u83b7\u53d6\u6570\u636e\u7684\u6280\u672f\uff0c\u901a\u8fc7\u6bd4\u8f83\u8868\u4e2d\u7684\u516c\u7528\u5217\u6765\u5173\u8054\u8868\u3002 \u5982\u4f55\u6267\u884c\u8054\u8868\u67e5\u8be2 \u8054\u8868\u67e5\u8be2\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a SELECT \u52171, \u52172, &#8230; FROM \u88681 INNER JOIN \u88682 ON \u88681.\u516c\u7528\u5217 = \u88682.\u516c\u7528\u5217 \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a SELECT \u8bed\u53e5\u6307\u5b9a\u8981\u4ece\u8868\u4e2d\u8fd4\u56de\u7684\u5217\u3002 [&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-5154","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5154","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=5154"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5154\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}