{"id":5359,"date":"2020-07-29T23:09:09","date_gmt":"2020-07-29T15:09:09","guid":{"rendered":"http:\/\/39.97.231.10\/?p=5359"},"modified":"2020-07-29T23:09:09","modified_gmt":"2020-07-29T15:09:09","slug":"mysql-join%e6%9f%a5%e8%af%a2%e5%86%85%e8%bf%9e%e6%8e%a5%ef%bc%8c%e5%a4%96%e8%bf%9e%e6%8e%a5%ef%bc%8c%e5%b7%a6%e8%bf%9e%e6%8e%a5%ef%bc%8c%e5%8f%b3%e8%bf%9e%e6%8e%a5","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=5359","title":{"rendered":"MySQL join\u67e5\u8be2(\u5185\u8fde\u63a5\uff0c\u5916\u8fde\u63a5\uff0c\u5de6\u8fde\u63a5\uff0c\u53f3\u8fde\u63a5)"},"content":{"rendered":"<div class=\\\"blog_title\\\">\n<div>\n<span style=\\\"font-size: 1rem;\\\">JOIN\u67e5\u8be2\u662f\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7684\u57fa\u672c\u529f\u80fd\uff0cMySQL\u63d0\u4f9b\u591a\u79cdJOIN\u5173\u8054\u67e5\u8be2\u65b9\u5f0f\uff0c\u5305\u62ec\u5185\u8fde\u63a5\uff0c\u5de6\u8fde\u63a5\uff0c\u53f3\u8fde\u63a5\u7b49\u7b49\uff0c\u672c\u6587\u5c06\u7b80\u5355\u4ecb\u7ecdMySQL JOIN\u67e5\u8be2\u7684\u5206\u7c7b\uff0c\u57fa\u672c\u8bed\u6cd5\u548c\u4f7f\u7528\u6848\u4f8b\u3002<\/span>\n<\/div>\n<\/div>\n<div class=\\\"blog_content\\\">\n<div class=\\\"blog_detail\\\">\n\u5728MySQL\u4e2d\uff0cJOIN\u53ef\u4ee5\u5206\u4e3a\u5185\u8fde\u63a5\u548c\u5916\u8fde\u63a5\uff0c\u5916\u8fde\u63a5\u53c8\u53ef\u4ee5\u5206\u4e3a\u5de6\u8fde\u63a5\u548c\u53f3\u8fde\u63a5\u3002<\/p>\n<ul>\n<li>join\u5185\u8fde\u63a5<\/li>\n<li>join\u5916\u8fde\u63a5\n<ul>\n<li>join\u5de6\u8fde\u63a5<\/li>\n<li>join\u53f3\u8fde\u63a5<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u5c06\u901a\u8fc7\u6848\u4f8b\u5bf9\u5185\u8fde\u63a5\uff0c\u5de6\u8fde\u63a5\u548c\u53f3\u8fde\u63a5\u7684\u4f7f\u7528\u8fdb\u884c\u8bf4\u660e\u3002\u6848\u4f8b\u6570\u636e\u5982\u4e0b\uff1a<\/p>\n<pre><code>mysql&gt; select * from t_a;\n+------+--------+\n| id_a | name_a |\n+------+--------+\n|    1 | a_1    |\n|    2 | a_2    |\n|    3 | a_3    |\n+------+--------+\n3 rows in set (0.01 sec)\nmysql&gt; select * from t_b;\n+------+--------+\n| id_b | name_b |\n+------+--------+\n|    2 | b_2    |\n|    3 | b_3    |\n|    4 | b_4    |\n+------+--------+\n3 rows in set (0.00 sec)\n<\/code><\/pre>\n<h4>1. join\u5185\u8fde\u63a5<\/h4>\n<p>\u5728MySQL\u4e2d\uff0c\u4e0b\u97623\u4e2a\u5185\u8fde\u63a5join\u5728\u8bed\u6cd5\u4e0a\u662f\u5b8c\u5168\u76f8\u7b49\u7684\u3002\u6ce8\u610f\u5728\u6807\u51c6SQL\u4e2d\uff0cINNER JOIN\u4e0eCROSS JOIN\u5e76\u4e0d\u7b49\u540c\u3002<\/p>\n<ul>\n<li>JOIN<\/li>\n<li>CROSS JOIN<\/li>\n<li>INNER JOIN<\/li>\n<\/ul>\n<p>\u5185\u8054JOIN\u6709\u591a\u79cd\u5199\u6cd5\uff0c\u4ee5\u4e0b\u67e5\u8be2\u7ed3\u679c\u76f8\u540c\u3002<br \/>\n\/\/ \u6ca1\u6709\u4f7f\u7528join\u5173\u952e\u5b57<br \/>\nselect * from t_a, t_b where t_a.id_a=t_b.id_b;<br \/>\n\/\/ join where<br \/>\nselect * from t_a\u00a0<strong>join<\/strong>\u00a0t_b\u00a0<strong>where<\/strong>\u00a0t_a.id_a=t_b.id_b;<br \/>\n\/\/ join on<br \/>\nselect * from t_a\u00a0<strong>join<\/strong>\u00a0t_b\u00a0<strong>on<\/strong>\u00a0t_a.id_a=t_b.id_b;<br \/>\n\/\/ inner join on<br \/>\nselect * from t_a\u00a0<strong>inner join<\/strong>\u00a0t_b\u00a0<strong>on<\/strong>\u00a0t_a.id_a=t_b.id_b;<br \/>\n\/\/ cross join on<br \/>\nselect * from t_a\u00a0<strong>cross join<\/strong>\u00a0t_b\u00a0<strong>on<\/strong>\u00a0t_a.id_a=t_b.id_b;<br \/>\n\/\/ cross join where<br \/>\nselect * from t_a\u00a0<strong>cross join<\/strong>\u00a0t_b\u00a0<strong>where<\/strong>\u00a0t_a.id_a=t_b.id_b;<br \/>\n\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>mysql&gt; select * from t_a inner join t_b on t_a.id_a=t_b.id_b;\n+------+--------+------+--------+\n| id_a | name_a | id_b | name_b |\n+------+--------+------+--------+\n|    2 | a_2    |    2 | b_2    |\n|    3 | a_3    |    3 | b_3    |\n+------+--------+------+--------+\n2 rows in set (0.01 sec)\n<\/code><\/pre>\n<h4>2. left join \u5de6\u8fde\u63a5<\/h4>\n<p>\u5de6\u8fde\u63a5join\uff0c\u4ee5\u5de6\u8fb9\u8868\u7684\u8fde\u63a5\u5b57\u6bb5\uff0c\u4f9d\u6b21\u5339\u914d\u53f3\u8fb9\u8868\uff0c\u5982\u679c\u5728\u53f3\u4fa7\u7684\u8868\u6ca1\u6709\u8bb0\u5f55\u5339\u914d\u4e0a\uff0c\u5219\u53f3\u5219\u8868\u7684\u8bb0\u5f55\u503c\u4ee5NULL\u586b\u5145\u3002<br \/>\nleft join\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code>mysql&gt; select * from t_a left join t_b on t_a.id_a=t_b.id_b;\n+------+--------+------+--------+\n| id_a | name_a | id_b | name_b |\n+------+--------+------+--------+\n|    2 | a_2    |    2 | b_2    |\n|    3 | a_3    |    3 | b_3    |\n|    1 | a_1    | NULL | NULL   |\n+------+--------+------+--------+\n3 rows in set (0.00 sec)\n<\/code><\/pre>\n<h4>3. right join \u53f3\u8fde\u63a5<\/h4>\n<p>\u53f3\u8fde\u63a5join\uff0c\u4ee5\u53f3\u8fb9\u8868\u7684\u8fde\u63a5\u5b57\u6bb5\uff0c\u4f9d\u6b21\u5339\u914d\u5de6\u8fb9\u8868\uff0c\u5982\u679c\u5728\u5de6\u4fa7\u7684\u8868\u6ca1\u6709\u8bb0\u5f55\u5339\u914d\u4e0a\uff0c\u5219\u5de6\u5219\u8868\u7684\u8bb0\u5f55\u503c\u4ee5NULL\u586b\u5145\u3002<br \/>\nright join\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code>mysql&gt; select * from t_a right join t_b on t_a.id_a=t_b.id_b;\n+------+--------+------+--------+\n| id_a | name_a | id_b | name_b |\n+------+--------+------+--------+\n|    2 | a_2    |    2 | b_2    |\n|    3 | a_3    |    3 | b_3    |\n| NULL | NULL   |    4 | b_4    |\n+------+--------+------+--------+\n3 rows in set (0.00 sec)<\/code><\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>JOIN\u67e5\u8be2\u662f\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7684\u57fa\u672c\u529f\u80fd\uff0cMySQL\u63d0\u4f9b\u591a\u79cdJOIN\u5173\u8054\u67e5\u8be2\u65b9\u5f0f\uff0c\u5305\u62ec\u5185\u8fde\u63a5\uff0c\u5de6\u8fde\u63a5\uff0c\u53f3\u8fde\u63a5\u7b49\u7b49\uff0c &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=5359\"> <span class=\"screen-reader-text\">MySQL join\u67e5\u8be2(\u5185\u8fde\u63a5\uff0c\u5916\u8fde\u63a5\uff0c\u5de6\u8fde\u63a5\uff0c\u53f3\u8fde\u63a5)<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[90,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"JOIN\u67e5\u8be2\u662f\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7684\u57fa\u672c\u529f\u80fd\uff0cMySQL\u63d0\u4f9b\u591a\u79cdJOIN\u5173\u8054\u67e5\u8be2\u65b9\u5f0f\uff0c\u5305\u62ec\u5185\u8fde\u63a5\uff0c\u5de6\u8fde\u63a5\uff0c\u53f3\u8fde\u63a5\u7b49\u7b49\uff0c&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5359"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5359"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5359\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5359"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}