{"id":4211,"date":"2018-12-14T20:58:00","date_gmt":"2018-12-14T12:58:00","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4211"},"modified":"2018-12-14T20:58:00","modified_gmt":"2018-12-14T12:58:00","slug":"mysql%e5%a4%9a%e8%a1%a8join%e8%bf%9e%e6%8e%a5%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4211.html","title":{"rendered":"MySQL\u591a\u8868join\u8fde\u63a5\u67e5\u8be2"},"content":{"rendered":"<p data-pm-slice=\"0 0 []\">=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u56e0\u4e3a\u5728\u5927\u5b66\u7684\u65f6\u5019SQL\u5b66\u7684\u4e0d\u624e\u5b9e\uff0c\u540e\u6765\u5de5\u4f5c\u4e2d\u4e5f\u6ca1\u6709\u4e13\u95e8\u6df1\u5165\u4e86\u89e3\u8fc7\uff0c\u867d\u7136\u4e00\u4e9b\u5e38\u89c4\/\u5e38\u89c1\u529f\u80fd\u7684SQL\u5199\u7684\u4e5f\u5f88\u6d41\u7545\uff0c\u4f46\u662f\u5728\u78b0\u5230\u590d\u6742\u573a\u666f\uff08\u9700\u8981\u591a\u4e2a\u8868\u5404\u79cdjoin\uff09\u7684\u65f6\u5019\u5c31\u4e0d\u884c\u4e86\uff0c\u6709\u70b9\u53d1\u865a\uff0c\u8fd9\u6b21\u8d81\u7740\u4e00\u4e2a\u673a\u4f1a\u597d\u597d\u5b66\u4e60\u7406\u89e3\u4e00\u4e0bMySQL\u591a\u8868join\u8fde\u63a5\u67e5\u8be2\u7684\u77e5\u8bc6\uff0c\u4e5f\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>\u5e38\u89c1\u7684 join \u6309\u7167\u529f\u80fd\u5927\u81f4\u5206\u4e3a\u5982\u4e0b\u4e09\u7c7b\uff1a<\/p>\n<ul>\n<li><strong>INNER JOIN\uff08\u5185\u8fde\u63a5\u6216\u7b49\u503c\u8fde\u63a5\uff09<\/strong>\uff1a\u83b7\u53d6\u4e24\u4e2a\u8868\u4e2d\u5b57\u6bb5\u5339\u914d\u5173\u7cfb\u7684\u8bb0\u5f55\u3002<\/li>\n<li><strong>LEFT JOIN\uff08\u5de6\u8fde\u63a5\uff09\uff1a<\/strong>\u83b7\u53d6\u5de6\u8868\u6240\u6709\u8bb0\u5f55\uff0c\u5373\u4f7f\u53f3\u8868\u6ca1\u6709\u5bf9\u5e94\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/li>\n<li><strong>RIGHT JOIN\uff08\u53f3\u8fde\u63a5\uff09\uff1a<\/strong>\u00a0\u4e0e LEFT JOIN \u76f8\u53cd\uff0c\u7528\u4e8e\u83b7\u53d6\u53f3\u8868\u6240\u6709\u8bb0\u5f55\uff0c\u5373\u4f7f\u5de6\u8868\u6ca1\u6709\u5bf9\u5e94\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<p>\u5148\u75282\u5f20\u56fe\u76f4\u89c2\u7684\u8868\u793a\u4e00\u4e0b\u5404\u79cdjoin\u7684\u4f5c\u7528\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4213\" src=\"https:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2018\/12\/Visual_SQL_JOINS_orig.jpg\" alt=\"\" width=\"966\" height=\"760\" srcset=\"https:\/\/ixyzero.com\/wp-content\/uploads\/2018\/12\/Visual_SQL_JOINS_orig.jpg 966w, https:\/\/ixyzero.com\/wp-content\/uploads\/2018\/12\/Visual_SQL_JOINS_orig-300x236.jpg 300w, https:\/\/ixyzero.com\/wp-content\/uploads\/2018\/12\/Visual_SQL_JOINS_orig-768x604.jpg 768w\" sizes=\"auto, (max-width: 966px) 100vw, 966px\" \/><\/p>\n<p>&amp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4212\" src=\"https:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2018\/12\/sql-joins.png\" alt=\"\" width=\"1520\" height=\"1164\" srcset=\"https:\/\/ixyzero.com\/wp-content\/uploads\/2018\/12\/sql-joins.png 1520w, https:\/\/ixyzero.com\/wp-content\/uploads\/2018\/12\/sql-joins-300x230.png 300w, https:\/\/ixyzero.com\/wp-content\/uploads\/2018\/12\/sql-joins-768x588.png 768w, https:\/\/ixyzero.com\/wp-content\/uploads\/2018\/12\/sql-joins-1024x784.png 1024w\" sizes=\"auto, (max-width: 1520px) 100vw, 1520px\" \/><\/p>\n<p>\u7136\u540e\u518d\u901a\u8fc7\u4e00\u4e9b\u672c\u5730\u73af\u5883\u9020\u6570\u636e\u8fdb\u884c\u6d4b\u8bd5\u4ee5\u52a0\u6df1\u5f71\u54cd\uff1a<\/p>\n<pre class=\"lang:default decode:true \">\/*\r\n\u521b\u5efa\u6d4b\u8bd5\u8868\r\n*\/\r\nCREATE DATABASE `do_test` \/*!40100 DEFAULT CHARACTER SET utf8 *\/;\r\nUSE do_test;\r\n\r\nCREATE TABLE `table1` (\r\n  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,\r\n  `name` varchar(100) NOT NULL,\r\n  `sex` varchar(100) NOT NULL,\r\n  `age` int(11) unsigned,\r\n  PRIMARY KEY (`id`)\r\n) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;\r\n\r\nCREATE TABLE `table2` (\r\n  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,\r\n  `name` varchar(100) NOT NULL,\r\n  `sex` varchar(100) NOT NULL,\r\n  `age` int(11) unsigned,\r\n  PRIMARY KEY (`id`)\r\n) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4\r\n\r\n\r\n\/*\r\n\u586b\u5145\u6d4b\u8bd5\u6570\u636e\r\n*\/\r\nINSERT INTO `table1`\r\n\t(name, age, sex)\r\nVALUES\r\n\t('Pirate', 22, 'male'),\r\n\t('Monkey', 19, 'male'),\r\n\t('Ninja', 26, 'female'),\r\n\t('Spaghetti', 25, 'male')\r\n;\r\n\r\nINSERT INTO `table2`\r\n\t(name, age, sex)\r\nVALUES\r\n\t('Rutabaga', 22, 'female'),\r\n\t('Pirate', 22, 'male'),\r\n\t('Darth Vader', 25, 'male'),\r\n\t('Ninja', 26, 'female')\r\n;\r\n\r\n\r\n\/*\r\n\u8fdb\u884c\u6d4b\u8bd5\r\n*\/\r\n-- \u4ee5\u4e0b\u4e24\u53e5SQL\u7684\u529f\u80fd\u7b49\u4ef7\r\nselect A.id,A.name,B.name from table1 A, table2 B where A.id = B.id;\r\nselect A.id,A.name,B.name from table1 A inner join table2 B on A.id = B.id;\r\n\r\n-- \u4ee5\u4e0b\u4e24\u53e5SQL\u7684\u529f\u80fd\u7b49\u4ef7\r\nselect A.id,A.name,B.name from table1 A, table2 B where A.name = B.name;\r\nselect A.id,A.name,B.name from table1 A inner join table2 B on A.name = B.name;\r\n\r\nselect * from table1 A left join table2 B on A.name = B.name;\r\n\r\nselect * from table1 A left join table2 B on A.name = B.name where A.id is null or B.id is null;\r\n\r\nselect * from table1 A right join table2 B on A.name = B.name;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li>Mysql Join\u8bed\u6cd5\u89e3\u6790\u4e0e\u6027\u80fd\u5206\u6790<br \/>\n<a href=\"https:\/\/www.cnblogs.com\/BeginMan\/p\/3754322.html\">https:\/\/www.cnblogs.com\/BeginMan\/p\/3754322.html<\/a><\/li>\n<li>MySQL JOIN \u591a\u8868\u8fde\u63a5<br \/>\n<a href=\"https:\/\/www.cnblogs.com\/xiaofeng028\/p\/4098965.html\">https:\/\/www.cnblogs.com\/xiaofeng028\/p\/4098965.html<\/a><br \/>\n<a href=\"http:\/\/www.5idev.com\/p-php_mysql_join_multi_table.shtml\">http:\/\/www.5idev.com\/p-php_mysql_join_multi_table.shtml<\/a><\/li>\n<li>Mysql \u8fde\u63a5\u7684\u4f7f\u7528 #<span style=\"color: #ff0000;\">\u6709\u63d0\u4f9b\u6d4b\u8bd5SQL<\/span><br \/>\n<a href=\"http:\/\/www.runoob.com\/mysql\/mysql-join.html\">http:\/\/www.runoob.com\/mysql\/mysql-join.html<\/a><\/li>\n<li>MySQL\u591a\u8868\u8fde\u63a5\u67e5\u8be2<br \/>\n<a href=\"https:\/\/www.cnblogs.com\/geaozhang\/p\/6753190.html\">https:\/\/www.cnblogs.com\/geaozhang\/p\/6753190.html<\/a><\/li>\n<li>MySQL\u4e2d\u7684\u5404\u79cdjoin<br \/>\n<a href=\"http:\/\/wxb.github.io\/2016\/12\/15\/MySQL%E4%B8%AD%E7%9A%84%E5%90%84%E7%A7%8Djoin.html\">http:\/\/wxb.github.io\/2016\/12\/15\/MySQL%E4%B8%AD%E7%9A%84%E5%90%84%E7%A7%8Djoin.html<\/a><\/li>\n<li>\u56fe\u89e3 SQL \u91cc\u7684\u5404\u79cd JOIN #<span style=\"color: #ff0000;\"><strong>nice<\/strong><\/span><br \/>\n<a href=\"https:\/\/mazhuang.org\/2017\/09\/11\/joins-in-sql\/\">https:\/\/mazhuang.org\/2017\/09\/11\/joins-in-sql\/<\/a><\/li>\n<li>MySQL\u591a\u8868\u5173\u8054\u67e5\u8be2\u6548\u7387\u9ad8\u70b9\u8fd8\u662f\u591a\u6b21\u5355\u8868\u67e5\u8be2\u6548\u7387\u9ad8\uff0c\u4e3a\u4ec0\u4e48\uff1f<br \/>\n<a href=\"https:\/\/www.zhihu.com\/question\/68258877\">https:\/\/www.zhihu.com\/question\/68258877<\/a><\/li>\n<li>Visual Representation of SQL Joins<br \/>\n<a href=\"https:\/\/www.codeproject.com\/Articles\/33052\/Visual-Representation-of-SQL-Joins\">https:\/\/www.codeproject.com\/Articles\/33052\/Visual-Representation-of-SQL-Joins<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u56e0\u4e3a\u5728\u5927\u5b66\u7684\u65f6\u5019SQL\u5b66\u7684\u4e0d\u624e\u5b9e\uff0c\u540e\u6765\u5de5\u4f5c\u4e2d\u4e5f\u6ca1\u6709\u4e13\u95e8\u6df1\u5165\u4e86\u89e3\u8fc7\uff0c\u867d\u7136\u4e00\u4e9b\u5e38\u89c4\/\u5e38\u89c1\u529f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,23,7],"tags":[421,1262],"class_list":["post-4211","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","category-programing","tag-join","tag-sql"],"views":12162,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4211"}],"version-history":[{"count":2,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4211\/revisions"}],"predecessor-version":[{"id":4215,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4211\/revisions\/4215"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}