浅析Sql中内连接、外连接、全连接、交叉连接的区别
<h1 style="color: black; text-align: left; margin-bottom: 10px;">外连接(out join)</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">外连接分为外左连接(left outer join)和外右连接(right outer join)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">注释:left outer join 与 left join 等价, <span style="color: black;">通常</span>写成left join right outer join 与 right join等价,<span style="color: black;">通常</span>写成right join</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">左连接</strong>,取左边的表的<span style="color: black;">所有</span>,右边的表按<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;">举例:select <select list> from A left join B on A.id=B.id</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/b02879cb05a44368ac7cca53bc0144d3~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338431&x-signature=5KbXNN6BbSjvviKXIXj8E6pOViQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">右连接</strong>:取右边的表的<span style="color: black;">所有</span>,左边的表按<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;">举例:select <select list> from A right join B on A.id=B.id</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/7427e9cda60e4372bed752e1fd8ab5a3~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338431&x-signature=d25RvS3Lqil4sB5h2WB6%2F7s7Z5s%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">内连接(inner join)</h1>
<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>的部分</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">注释:inner join 就等于 join </p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/c2c0adeda2744f06819a9ef0920137e8~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338431&x-signature=hL4wGZvzt3vtqi0grQN7MnjCB%2BQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">必须</span>C/C++ Linux服务器架构师学习资料私信“资料”(资料<span style="color: black;">包含</span>C/C++,Linux,golang技术,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流<span style="color: black;">媒介</span>,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK,ffmpeg等),免费分享</strong></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/7ff6b7a73a8242ccaa868681bc642dc8~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338431&x-signature=IIgfanGo6lSofxbScwoS96%2FKKx4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">交叉连接(CROSS JOIN)</h1>
<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>在数学中,两个集合X和Y的笛卡尓积(Cartesian product),又<span style="color: black;">叫作</span>直积,<span style="color: black;">暗示</span>为X × Y,<span style="color: black;">第1</span>个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员</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>有两个集合A和B。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">A = {0,1} B = {2,3,4}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">集合 A×B 和 B×A的结果集就<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;">A×B = {(0,2),(1,2),(0,3),(1,3),(0,4),(1,4)};</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">B×A = {(2,0),(2,1),(3,0),(3,1),(4,0),(4,1)};</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上A×B和B×A的结果就<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;">1,两个集合相乘,不满足交换率,既 A×B ≠ B×A;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2,A集合和B集合相乘,<span style="color: black;">包括</span>了集合A中元素和集合B中元素相结合的所有的可能性。既两个集合相乘得到的新集合的元素个数是 A集合的元素个数 × B集合的元素个数;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">交叉连接有两种,显式的和隐式的,不带ON子句,返回的是两表的乘积,<span style="color: black;">亦</span>叫笛卡尔积。例如:下面的语句1和语句2的结果是相同的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">语句1:隐式的交叉连接,<span style="color: black;">无</span>CROSS JOIN。</strong>SELECT O.ID, O.ORDER_NUMBER, C.ID, C.NAMEFROM ORDERS O , CUSTOMERS CWHERE O.ID=1;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">语句2:显式的交叉连接,<span style="color: black;">运用</span>CROSS JOIN。</strong>SELECT O.ID,O.ORDER_NUMBER,C.ID,C.NAMEFROM ORDERS O CROSS JOIN CUSTOMERS CWHERE O.ID=1; </p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">内连接和where的区别:</h1>
<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>看成是在做乘法运算。</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>数据库中有两张表,student表和 student_subject表,如下所示:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/30bb41a2a20f49b5983e7a8480cabbf5~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338431&x-signature=17lLLp1tkLdwW6rozc7sV1pac%2FM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/36c35706b2494667baa3e617d90cc716~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338431&x-signature=bi%2FKGeEuuaS%2B12%2FhWFL5WKPcMEc%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>执行以下的sql语句,只是纯粹的进行表连接。</p><span style="color: black;">SELECT</span> * <span style="color: black;">from</span> student <span style="color: black;">JOIN</span> student_subject;
<span style="color: black;">SELECT</span> * <span style="color: black;">from</span>student_subject<span style="color: black;">JOIN</span> student;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">看一下执行结果:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/a8db54536e054943aaba74a2e15b165b~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338431&x-signature=fnV4YAoCcW7bfoi4%2FNH0xwb89c8%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 表1.0 </p>
</div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/4d9983ef38124cbfb7cbc1c63993fd6f~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723338431&x-signature=XLH8mDA44miyuLEIx1XcPdL4QKI%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">表1.1</p>
</div>
<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;">以<span style="color: black;">第1</span>条sql语句为例<span style="color: black;">咱们</span>来看一下他的执行流程,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1,from语句把student表 和 student_subject表从数据库文件加载到内存中。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2,join语句相当于对两张表做了乘法运算,把student表中的每一行记录<span style="color: black;">根据</span><span style="color: black;">次序</span>和student_subject表中记录依次匹配。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3,匹配完成后,<span style="color: black;">咱们</span>得到了一张有 (student中记录数 × student_subject表中记录数)条的临时表。 在内存中形成的临时表如表1.0所示。<span style="color: black;">咱们</span>又把内存中表1.0所示的表<span style="color: black;">叫作</span>为‘笛卡尔积表’。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">再看一下sql中<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;">from on join where group by having select distinct union order by </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">咱们</span>看到on是在join和where前面的</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>的时候<span style="color: black;">通常</span>都会<span style="color: black;">运用</span>JOIN xxx ON xxx的语法,ON语句的执行是在JOIN语句之前的,<span style="color: black;">亦</span><span style="color: black;">便是</span>说两张表数据行之间进行匹配的时候,会先判断数据行<span style="color: black;">是不是</span>符合ON语句后面的<span style="color: black;">要求</span>,再决定<span style="color: black;">是不是</span>JOIN。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此呢</span>,有一个显而易见的SQL优化的<span style="color: black;">方法</span>是,当两张表的数据量比较大,又<span style="color: black;">必须</span>连接<span style="color: black;">查找</span>时,应该<span style="color: black;">运用</span> FROM table1 JOIN table2 ON xxx的语法,避免<span style="color: black;">运用</span> FROM table1,table2 WHERE xxx 的语法,<span style="color: black;">由于</span>后者会在内存中先生成一张数据量比<span style="color: black;">很强</span>的笛卡尔积表,<span style="color: black;">增多</span>了内存的开销。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">全连接(Full join)</h1>
<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>了join两侧表中所有满足检索<span style="color: black;">要求</span>的行</p>
认真阅读了楼主的帖子,非常有益。 论坛外链网http://www.fok120.com/ 你的见解独到,让我受益匪浅,非常感谢。 楼主继续加油啊!外链论坛加油! 楼主节操掉了,还不快捡起来! 论坛的成果是显著的,但我们不能因为成绩而沾沾自喜。 论坛的成功是建立在我们诚恳、务实、高效、创新和团结合作基础上,我们要把这种精神传递下去。 楼主听话,多发外链好处多,快到碗里来!外链论坛 http://www.fok120.com/ 楼主听话,多发外链好处多,快到碗里来!外链论坛 http://www.fok120.com/
页:
[1]