{"id":1107,"date":"2019-04-20T21:22:05","date_gmt":"2019-04-20T13:22:05","guid":{"rendered":"https:\/\/freesilo.com\/?p=1107"},"modified":"2019-04-20T21:22:05","modified_gmt":"2019-04-20T13:22:05","slug":"%e4%b8%80%e5%bc%a0%e5%9b%be%e7%9c%8b%e6%87%82-sql-%e7%9a%84%e5%90%84%e7%a7%8d-join-%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/freesilo.com\/?p=1107","title":{"rendered":"\u4e00\u5f20\u56fe\u770b\u61c2 SQL \u7684\u5404\u79cd join \u7528\u6cd5"},"content":{"rendered":"<p>\u4f5c\u8005\u4e28C.L. Moffatt<\/p>\n<p><a href=\"http:\/\/www.codeproject.com\/Articles\/33052\/Visual-Representation-of-SQL-Joins\">http:\/\/www.codeproject.com\/Articles\/33052\/Visual-Representation-of-SQL-Joins<\/a><\/p>\n<p>\u4e0b\u56fe\u5c55\u793a\u4e86 LEFT JOIN\u3001RIGHT JOIN\u3001INNER JOIN\u3001OUTER JOIN \u76f8\u5173\u7684 7 \u79cd\u7528\u6cd5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/freesilo.com\/wp-content\/uploads\/2019\/04\/d69693fd5d444196c2978673a59d5e72.jpg\" \/><\/p>\n<p>\u5177\u4f53\u5206\u89e3\u5982\u4e0b\uff1a<\/p>\n<p>1.INNER JOIN\uff08\u5185\u8fde\u63a5\uff09<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/freesilo.com\/wp-content\/uploads\/2019\/04\/dacfb6b4a93e8c0996af0a50896b2ad6.png\" alt=\"INNER_JOIN.png\" \/><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT\r\nFROM\u00a0Table_A\u00a0A\r\nINNER\u00a0JOIN\u00a0Table_B\u00a0B\r\nON\u00a0A.Key\u00a0=\u00a0B.Key\r\n<\/pre>\n<p>2.LEFT JOIN\uff08\u5de6\u8fde\u63a5\uff09<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/freesilo.com\/wp-content\/uploads\/2019\/04\/b2f77eff2e65e96b41d17cef95c040ca.png\" alt=\"LEFT_JOIN.png\" \/><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT \r\nFROM Table_A A\r\nLEFT JOIN Table_B B\r\nON A.Key = B.Key\r\n<\/pre>\n<p>3.RIGHT JOIN\uff08\u53f3\u8fde\u63a5\uff09<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/freesilo.com\/wp-content\/uploads\/2019\/04\/c4f5f384bdf32867d5b9fb1f7c074c67.png\" alt=\"RIGHT_JOIN.png\" \/><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT \r\nFROM Table_A A\r\nRIGHT JOIN Table_B B\r\nON A.Key = B.Key\r\n<\/pre>\n<p>4.OUTER JOIN\uff08\u5916\u8fde\u63a5\uff09<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/freesilo.com\/wp-content\/uploads\/2019\/04\/8d3432ce524d300cc065599dee28fc51.png\" alt=\"FULL_OUTER_JOIN.png\" \/><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT \r\nFROM Table_A A\r\nFULL OUTER JOIN Table_B B\r\nON A.Key = B.Key\r\n<\/pre>\n<p>5.LEFT JOIN EXCLUDING INNER JOIN\uff08\u5de6\u8fde\u63a5-\u5185\u8fde\u63a5\uff09<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/freesilo.com\/wp-content\/uploads\/2019\/04\/c650015c237c8c107148dc3ad2081519.png\" alt=\"LEFT_EXCLUDING_JOIN.png\" \/><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT  \r\nFROM Table_A A\r\nLEFT JOIN Table_B B\r\nON A.Key = B.Key\r\nWHERE B.Key IS NULL\r\n<\/pre>\n<p>6.RIGHT JOIN EXCLUDING INNER JOIN\uff08\u53f3\u8fde\u63a5-\u5185\u8fde\u63a5\uff09<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/freesilo.com\/wp-content\/uploads\/2019\/04\/51e7ee0f57359e52a9d249c9cfee1250.png\" alt=\"RIGHT_EXCLUDING_JOIN.png\" \/><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT \r\nFROM Table_A A\r\nRIGHT JOIN Table_B B\r\nON A.Key = B.Key\r\nWHERE A.Key IS NULL\r\n<\/pre>\n<p>7.OUTER JOIN EXCLUDING INNER JOIN\uff08\u5916\u8fde\u63a5-\u5185\u8fde\u63a5\uff09<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/freesilo.com\/wp-content\/uploads\/2019\/04\/05f3dd4ad70f9710b9657bd0874ca484.png\" alt=\"OUTER_EXCLUDING_JOIN.png\" \/><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT \r\nFROM Table_A A\r\nFULL OUTER JOIN Table_B B\r\nON A.Key = B.Key\r\nWHERE A.Key IS NULL OR B.Key IS NULL\r\n<\/pre>\n<p><strong>Examples<\/strong><br \/>\nSuppose we have two tables, Table_A and Table_B. The data in these tables are shown below:<\/p>\n<blockquote><p>TABLE_A<br \/>\nPK Value<br \/>\n&#8212;- &#8212;&#8212;&#8212;-<br \/>\n1 FOX<br \/>\n2 COP<br \/>\n3 TAXI<br \/>\n6 WASHINGTON<br \/>\n7 DELL<br \/>\n5 ARIZONA<br \/>\n4 LINCOLN<br \/>\n10 LUCENT<\/p>\n<p>TABLE_B<br \/>\nPK Value<br \/>\n&#8212;- &#8212;&#8212;&#8212;-<br \/>\n1 TROT<br \/>\n2 CAR<br \/>\n3 CAB<br \/>\n6 MONUMENT<br \/>\n7 PC<br \/>\n8 MICROSOFT<br \/>\n9 APPLE<br \/>\n11 SCOTCH<\/p><\/blockquote>\n<p>The results of the seven Joins are shown below:<\/p>\n<blockquote>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n-- INNER JOIN\r\nSELECT A.PK AS A_PK, A.Value AS A_Value,\r\nB.Value AS B_Value, B.PK AS B_PK\r\nFROM Table_A A\r\nINNER JOIN Table_B B\r\nON A.PK = B.PK\r\n<\/pre>\n<p>A_PK A_Value B_Value B_PK<br \/>\n&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;-<br \/>\n1 FOX TROT 1<br \/>\n2 COP CAR 2<br \/>\n3 TAXI CAB 3<br \/>\n6 WASHINGTON MONUMENT 6<br \/>\n7 DELL PC 7<\/p>\n<p>(5 row(s) affected)\n<\/p><\/blockquote>\n<blockquote>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n-- LEFT JOIN\r\nSELECT A.PK AS A_PK, A.Value AS A_Value,\r\nB.Value AS B_Value, B.PK AS B_PK\r\nFROM Table_A A\r\nLEFT JOIN Table_B B\r\nON A.PK = B.PK\r\n<\/pre>\n<p>A_PK A_Value B_Value B_PK<br \/>\n&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;-<br \/>\n1 FOX TROT 1<br \/>\n2 COP CAR 2<br \/>\n3 TAXI CAB 3<br \/>\n4 LINCOLN NULL NULL<br \/>\n5 ARIZONA NULL NULL<br \/>\n6 WASHINGTON MONUMENT 6<br \/>\n7 DELL PC 7<br \/>\n10 LUCENT NULL NULL<\/p>\n<p>(8 row(s) affected)\n<\/p><\/blockquote>\n<blockquote>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n-- RIGHT JOIN\r\nSELECT A.PK AS A_PK, A.Value AS A_Value,\r\nB.Value AS B_Value, B.PK AS B_PK\r\nFROM Table_A A\r\nRIGHT JOIN Table_B B\r\nON A.PK = B.PK\r\n<\/pre>\n<p>A_PK A_Value B_Value B_PK<br \/>\n&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;-<br \/>\n1 FOX TROT 1<br \/>\n2 COP CAR 2<br \/>\n3 TAXI CAB 3<br \/>\n6 WASHINGTON MONUMENT 6<br \/>\n7 DELL PC 7<br \/>\nNULL NULL MICROSOFT 8<br \/>\nNULL NULL APPLE 9<br \/>\nNULL NULL SCOTCH 11<\/p>\n<p>(8 row(s) affected)\n<\/p><\/blockquote>\n<blockquote>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n-- OUTER JOIN\r\nSELECT A.PK AS A_PK, A.Value AS A_Value,\r\nB.Value AS B_Value, B.PK AS B_PK\r\nFROM Table_A A\r\nFULL OUTER JOIN Table_B B\r\nON A.PK = B.PK\r\n<\/pre>\n<p>A_PK A_Value B_Value B_PK<br \/>\n&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;-<br \/>\n1 FOX TROT 1<br \/>\n2 COP CAR 2<br \/>\n3 TAXI CAB 3<br \/>\n6 WASHINGTON MONUMENT 6<br \/>\n7 DELL PC 7<br \/>\nNULL NULL MICROSOFT 8<br \/>\nNULL NULL APPLE 9<br \/>\nNULL NULL SCOTCH 11<br \/>\n5 ARIZONA NULL NULL<br \/>\n4 LINCOLN NULL NULL<br \/>\n10 LUCENT NULL NULL<\/p>\n<p>(11 row(s) affected)\n<\/p><\/blockquote>\n<blockquote>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n-- LEFT EXCLUDING JOIN\r\nSELECT A.PK AS A_PK, A.Value AS A_Value,\r\nB.Value AS B_Value, B.PK AS B_PK\r\nFROM Table_A A\r\nLEFT JOIN Table_B B\r\nON A.PK = B.PK\r\nWHERE B.PK IS NULL\r\n<\/pre>\n<p>A_PK A_Value B_Value B_PK<br \/>\n&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;-<br \/>\n4 LINCOLN NULL NULL<br \/>\n5 ARIZONA NULL NULL<br \/>\n10 LUCENT NULL NULL<\/p>\n<p>(3 row(s) affected)\n<\/p><\/blockquote>\n<blockquote>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n-- RIGHT EXCLUDING JOIN\r\nSELECT A.PK AS A_PK, A.Value AS A_Value,\r\nB.Value AS B_Value, B.PK AS B_PK\r\nFROM Table_A A\r\nRIGHT JOIN Table_B B\r\nON A.PK = B.PK\r\nWHERE A.PK IS NULL\r\n<\/pre>\n<p>A_PK A_Value B_Value B_PK<br \/>\n&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;-<br \/>\nNULL NULL MICROSOFT 8<br \/>\nNULL NULL APPLE 9<br \/>\nNULL NULL SCOTCH 11<\/p>\n<p>(3 row(s) affected)\n<\/p><\/blockquote>\n<blockquote>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n-- OUTER EXCLUDING JOIN\r\nSELECT A.PK AS A_PK, A.Value AS A_Value,\r\nB.Value AS B_Value, B.PK AS B_PK\r\nFROM Table_A A\r\nFULL OUTER JOIN Table_B B\r\nON A.PK = B.PK\r\nWHERE A.PK IS NULL\r\nOR B.PK IS NULL\r\n<\/pre>\n<p>A_PK A_Value B_Value B_PK<br \/>\n&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;-<br \/>\nNULL NULL MICROSOFT 8<br \/>\nNULL NULL APPLE 9<br \/>\nNULL NULL SCOTCH 11<br \/>\n5 ARIZONA NULL NULL<br \/>\n4 LINCOLN NULL NULL<br \/>\n10 LUCENT NULL NULL<\/p>\n<p>(6 row(s) affected)\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\u4e28C.L. Moffatt http:\/\/www.codeproject.c&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/freesilo.com\/?p=1107\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-1107","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/1107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1107"}],"version-history":[{"count":5,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/1107\/revisions"}],"predecessor-version":[{"id":1128,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/1107\/revisions\/1128"}],"wp:attachment":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}