MySQL各样连接详解(自然连接,内连接,外连接的区别join)
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在MySQL中,多表连接的语法格式如下:</span></p><span style="color: black;">SELECT <span style="color: black;"><<span style="color: black;"><span style="color: black;">查找</span>列表</span>></span></span><span style="color: black;">FROM <span style="color: black;"><<span style="color: black;">表名1</span>></span> [连接类型] </span><span style="color: black;">JOIN <span style="color: black;"><<span style="color: black;">表名2</span>></span> ON <span style="color: black;"><<span style="color: black;">连接<span style="color: black;">要求</span></span>></span></span><span style="color: black;">WHERE <span style="color: black;"><<span style="color: black;"><span style="color: black;">查找</span><span style="color: black;">要求</span></span>></span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其中连接类型有四种:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">内连接(inner join)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">自然连接(nature join)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">交叉连接(cross join)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外连接(outer join)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">多表连接<span style="color: black;">常常</span>要<span style="color: black;">经过</span>表之间的<span style="color: black;">理学</span>外键(<span style="color: black;">或</span><span style="color: black;">规律</span>外键)进行<span style="color: black;">相关</span><span style="color: black;">查找</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">接下来就分别介绍一下这几个连接的<span style="color: black;">详细</span>用法和区别。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">表数据准备</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">table1:</p>
<span style="color: black;"><span style="color: black;">CREATE</span> <span style="color: black;">TABLE</span> <span style="color: black;">`table1`</span>(</span><span style="color: black;"><span style="color: black;">`a`</span> <span style="color: black;">VARCHAR</span>(<span style="color: black;">20</span>),</span><span style="color: black;"><span style="color: black;">`b`</span> <span style="color: black;">VARCHAR</span>(<span style="color: black;">20</span>) <span style="color: black;">NOT</span> <span style="color: black;">NULL</span> <span style="color: black;">DEFAULT</span> ,</span><span style="color: black;"><span style="color: black;">`c`</span> <span style="color: black;">VARCHAR</span>(<span style="color: black;">20</span>) <span style="color: black;">NOT</span> <span style="color: black;">NULL</span> <span style="color: black;">DEFAULT</span> ,</span><span style="color: black;"><span style="color: black;">`g`</span> <span style="color: black;">VARCHAR</span>(<span style="color: black;">20</span>) <span style="color: black;">NOT</span> <span style="color: black;">NULL</span> <span style="color: black;">DEFAULT</span> ,</span><span style="color: black;">PRIMARY <span style="color: black;">KEY</span>(<span style="color: black;">`a`</span>)</span><span style="color: black;">);</span><span style="color: black;"><span style="color: black;">insert</span> <span style="color: black;">into</span> table1 <span style="color: black;">values</span>(<span style="color: black;">1</span> , <span style="color: black;">2</span> , <span style="color: black;">3</span>,<span style="color: black;">3</span>);</span><span style="color: black;"><span style="color: black;">insert</span> <span style="color: black;">into</span> table1 <span style="color: black;">values</span>(<span style="color: black;">2</span> , <span style="color: black;">3</span> , <span style="color: black;">4</span>,<span style="color: black;">4</span>);</span><span style="color: black;"><span style="color: black;">insert</span> <span style="color: black;">into</span> table1 <span style="color: black;">values</span>(<span style="color: black;">5</span> , <span style="color: black;">6</span> , <span style="color: black;">7</span>,<span style="color: black;">4</span>);</span><span style="color: black;"><span style="color: black;">insert</span> <span style="color: black;">into</span> table1 <span style="color: black;">values</span>(<span style="color: black;">8</span> , <span style="color: black;">9</span> , <span style="color: black;">10</span>,<span style="color: black;">5</span>);</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">table2:</span></p><span style="color: black;"><span style="color: black;">CREATE</span> <span style="color: black;">TABLE</span> <span style="color: black;">`table2`</span>(</span><span style="color: black;"><span style="color: black;">`c`</span> <span style="color: black;">VARCHAR</span>(<span style="color: black;">20</span>),</span><span style="color: black;"><span style="color: black;">`d`</span> <span style="color: black;">VARCHAR</span>(<span style="color: black;">20</span>) <span style="color: black;">NOT</span> <span style="color: black;">NULL</span> <span style="color: black;">DEFAULT</span> ,</span><span style="color: black;"><span style="color: black;">`e`</span> <span style="color: black;">VARCHAR</span>(<span style="color: black;">20</span>) <span style="color: black;">NOT</span> <span style="color: black;">NULL</span> <span style="color: black;">DEFAULT</span> ,</span><span style="color: black;"><span style="color: black;">`g`</span> <span style="color: black;">VARCHAR</span>(<span style="color: black;">20</span>) <span style="color: black;">NOT</span> <span style="color: black;">NULL</span> <span style="color: black;">DEFAULT</span> </span><span style="color: black;">);</span><span style="color: black;"><span style="color: black;">insert</span> <span style="color: black;">into</span> table2 <span style="color: black;">values</span>(<span style="color: black;">3</span> , <span style="color: black;">4</span> , <span style="color: black;">5</span>,<span style="color: black;">3</span>);</span><span style="color: black;"><span style="color: black;">insert</span> <span style="color: black;">into</span> table2 <span style="color: black;">values</span>(<span style="color: black;">4</span> , <span style="color: black;">5</span> , <span style="color: black;">6</span>,<span style="color: black;">4</span>);</span><span style="color: black;"><span style="color: black;">insert</span> <span style="color: black;">into</span> table2 <span style="color: black;">values</span>(<span style="color: black;">8</span> , <span style="color: black;">9</span> , <span style="color: black;">1</span>,<span style="color: black;">5</span>);</span><span style="color: black;"><span style="color: black;">insert</span> <span style="color: black;">into</span> table2 <span style="color: black;">values</span>(<span style="color: black;">10</span> , <span style="color: black;">11</span> , <span style="color: black;">12</span>,<span style="color: black;">6</span>);</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">内连接:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在数据库多表<span style="color: black;">相关</span><span style="color: black;">查找</span>中,内连接是最常用的连接类型了,当然<span style="color: black;">亦</span>是默认的连接类型。<span style="color: black;">能够</span>在FROM子句中<span style="color: black;">运用</span>INNER JOIN(INNER关键字<span style="color: black;">能够</span>省略)来实现内连接</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/drBYxd3icibao5yywt35cm7L89utpic948lHJgfGRODvicnADIrY8NOed2amz5nS2tVyapiaYWjbRonUBH7qc8yo8Xg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如上图inner join<span style="color: black;">便是</span>取A和B的交集</p><span style="color: black;"><span style="color: black;">select</span> * <span style="color: black;">from</span> table1 e <span style="color: black;">inner</span> <span style="color: black;">join</span> table2 f <span style="color: black;">on</span> e.c=f.c;</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/drBYxd3icibao5yywt35cm7L89utpic948lRhJXiafFW3WQQvG4QRcjY0HHP0LiapU2D2hVibaxNG1qpPCIGJkAnHG7w/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">自然连接:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">自然连接是一种特殊的等值连接,他<span style="color: black;">需求</span>两个关系表中进行比较的<span style="color: black;">必要</span>是相同的属性列,无须添加连接<span style="color: black;">要求</span>,并且在结果中<strong style="color: blue;">消除重复的属性列</strong>。</span></p><span style="color: black;"><span style="color: black;">Select</span> * <span style="color: black;">from</span> table1 <span style="color: black;">natural</span> <span style="color: black;">join</span> table2;</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/drBYxd3icibao5yywt35cm7L89utpic948lR7g5vSbibderMCa6DzickGZw81n3SlNAWpB50eJc453kkQYLib8x05oibg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">注:去除重复是<span style="color: black;">按照</span>表table1和表table2中都有的列组合起来(c+g)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">交叉连接:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">MySQL cross join是mysql中的一种连接方式,区别于内连接和外连接,<span style="color: black;">针对</span>cross join连接<span style="color: black;">来讲</span>,其实<span style="color: black;">运用</span>的<span style="color: black;">便是</span>笛卡尔连接</span></p><span style="color: black;"><span style="color: black;">Select</span> * <span style="color: black;">from</span> table1 <span style="color: black;">CROSS</span> <span style="color: black;">join</span> table2;</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/drBYxd3icibao5yywt35cm7L89utpic948lMxxsy5g0Wam6rwjJJP34QcwP1EedOyNsLU3ibu6SASJOfAtRE6HA3Aw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外连接:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>外连接时,以主表中每行的数据去匹配从表中的数据行,<span style="color: black;">倘若</span>符合连接<span style="color: black;">要求</span>则返回到结果集中;<span style="color: black;">倘若</span><span style="color: black;">无</span>找到匹配行,则主表的行仍然<span style="color: black;">保存</span>,并且返回到结果集中,相应的从表中的数据行被填上NULL值后<span style="color: black;">亦</span>返回到结果集中。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外连接有3种类型,分别是:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">左外连接(LEFT OUTER JOIN)</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">右外连接(RIGHT OUTER JOIN)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">全外连接(FULL OUTER JOIN)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>,在MySQL中,是不支持全外连接的。这一点要<span style="color: black;">重视</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外链接-左外连接</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">左外连接的结果集中<span style="color: black;">包括</span>左表(JOIN关键字左边的表)中所有的记录,<span style="color: black;">倘若</span>右表中<span style="color: black;">无</span>满足连接<span style="color: black;">要求</span>的记录,则结果集中右表中的相应行数据填充为NULL</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/drBYxd3icibao5yywt35cm7L89utpic948lxUf52gbeykd69sxE3KQiaXpkad7tlEhdebTbAV6EDdZ3uv3iausrrzRg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">select</span> * <span style="color: black;">from</span> table1 e <span style="color: black;">left</span> <span style="color: black;">join</span> table2 f <span style="color: black;">on</span> e.c = f.c;</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/drBYxd3icibao5yywt35cm7L89utpic948l9ziaWdnrfDicRNWo3j4agnxw7aCibzK1L6Yp8xKasZ9B5louqFiannUicug/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外链接-右外连接</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">右外连接的结果集中,<span style="color: black;">包括</span>满足连接<span style="color: black;">要求</span>的所有数据,<span style="color: black;">另外</span>还<span style="color: black;">包含</span>右表(JOIN关键字右边的表)中不满足<span style="color: black;">要求</span>的数据,<span style="color: black;">此时</span>左表中的相应行数据为NULL</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/drBYxd3icibao5yywt35cm7L89utpic948l7Ohj80BuiaNFIRGurl5pwaftp7yyeMV5JdQ8j60tjGalGT84Pcwqiciag/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">select</span> * <span style="color: black;">from</span> table1 e <span style="color: black;">right</span> <span style="color: black;">OUTER</span> <span style="color: black;">join</span> table2 f <span style="color: black;">on</span> e.c = f.c;</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/drBYxd3icibao5yywt35cm7L89utpic948lVibzADKKwjx24N4obHeUiagpKWes5jiafXr4aB2ENpopdsrEyfX8tAMGg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外连接-全外连接</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">左表和右表都不做限制,所有的记录都<span style="color: black;">表示</span>,两表不足的<span style="color: black;">地区</span>用null 填充;<span style="color: black;">亦</span><span style="color: black;">便是</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">左外连接 = 左表<span style="color: black;">所有</span>记录 + <span style="color: black;">关联</span>联结果</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">右外连接 = 右表<span style="color: black;">所有</span>记录 + <span style="color: black;">关联</span>联结果</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">全外连接 = 左表<span style="color: black;">所有</span>记录 + 右表<span style="color: black;">所有</span>记录 + <span style="color: black;">关联</span>联结果 = 左外连接 + 右外连接 - <span style="color: black;">关联</span>联结果(即去重复)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/drBYxd3icibao5yywt35cm7L89utpic948lqVXCvMEwiaibeHfwrUyZ44ycMFGXeMEYxhggVNzszJ0VcOefDCDZ3cWg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">select</span> * <span style="color: black;">from</span> table1 e <span style="color: black;">left</span> <span style="color: black;">OUTER</span> <span style="color: black;">join</span>table2 f<span style="color: black;">on</span> e.c = f.c</span><span style="color: black;"><span style="color: black;">union</span></span><span style="color: black;"><span style="color: black;">select</span> * <span style="color: black;">from</span> table1 e <span style="color: black;">right</span> <span style="color: black;">OUTER</span> <span style="color: black;">join</span> table2 f <span style="color: black;">on</span> e.c = f.c;</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/drBYxd3icibao5yywt35cm7L89utpic948lgvibVx0YsogluODKQibhxLq4B0VQt1J1FI64bm39rs8iaUtRYOiaND3Kog/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
感谢楼主的分享!我学到了很多。 “BS”(鄙视的缩写)
页:
[1]