b1gc8v 发表于 2024-8-4 10:39:27

内连接、外连接、左连接、右连接、等值连接、自然连接和自连接之间的区别,看这篇就够了!


    <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 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 style="color: black;">优秀</span><span style="color: black;">文案</span>,<span style="color: black;">第1</span>时间送达</span></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><strong style="color: blue;">pay</strong><span style="color: black;">或</span><strong style="color: blue;">mall</strong><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 style="color: black;">源自</span>:</span><span style="color: black;">blog.csdn.net/qq_44756792</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">/article/details/95503303</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>总结。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">首要</span>大概认识<span style="color: black;">各样</span>连接的关系和由来:</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">表之间的连接常有以下两种:</p>在SELECT语句的WHERE子句中<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>是&lt;、&lt;=、=、&gt;、&gt;=、!=、&lt;&gt;、!&lt; 和 !&gt;,当比较符为“=”时,<span style="color: black;">便是</span>等值连接,等值连接的结果中有重复列,在<span style="color: black;">目的</span>列中去除相同的字段名<span style="color: black;">便是</span>自然连接。以JOIN关键字指定的连接,T-SQL扩展了以JOIN关键字指定连接的<span style="color: black;">暗示</span>方式,使表的连接运算能力有所<span style="color: black;">加强</span>,以JOIN关键字指定的连接有三种类型:内连接、外连接、交叉连接(笛卡尔积)。<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">下面<span style="color: black;">仔细</span>分析<span style="color: black;">各样</span>连接方式的特点:</span></h2>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1.等值连接(相等连接):</span></h3><span style="color: black;">SELECT</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">XSB.*&nbsp;,&nbsp;CJB.*</p>&nbsp;<span style="color: black;">FROM</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;XSB&nbsp;,&nbsp;CJB</p>&nbsp;<span style="color: black;">WHERE</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;XSB.学号&nbsp;=&nbsp;CJB.学号</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>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2.自然连接:</span></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">数据库应用中最常用的是“自然连接”,它在<span style="color: black;">目的</span>列中去除相同的字段名。</p><span style="color: black;">SELECT</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;XSB.*&nbsp;,&nbsp;CJB.课程号,&nbsp;CJB.成绩</p><span style="color: black;">FROM</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;XSB&nbsp;,&nbsp;CJB</p><span style="color: black;">WHERE</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;XSB.学号=&nbsp;CJB.学号</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>属性上进行等值连接后,再去除重复的属性后所得的新表。</p>等值连接和自然连接的区别:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1)等值连接中不<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>进行自然连接。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2)等值连接不将重复属性去掉,而自然连接去掉重复属性,<span style="color: black;">亦</span><span style="color: black;">能够</span>说,自然连接是去掉重复列的等值连接。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3.内连接</span></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">指定了INNER关键字的连接是内连接,内连接<span style="color: black;">根据</span>ON所指定的连接<span style="color: black;">要求</span>合并两个表,返回满足<span style="color: black;">要求</span>的行。内连接是系统默认的,<span style="color: black;">能够</span>省略INNER关键字。<span style="color: black;">运用</span>内连接后仍可<span style="color: black;">运用</span>WHERE子句指定<span style="color: black;">要求</span>。扩展:超全的数据库建表/SQL/索引规范,适合贴在工位上!</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例1</p><span style="color: black;">SELECT</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;*</p>&nbsp;<span style="color: black;">FROM</span>&nbsp;&nbsp;XSB&nbsp;&nbsp;<span style="color: black;">INNER</span>&nbsp;&nbsp;<span style="color: black;">JOIN</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;CJB&nbsp;</p>&nbsp;&nbsp;&nbsp;<span style="color: black;">ON</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;XSB.学号&nbsp;=CJB.学号</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例2</p><span style="color: black;">SELECT</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;姓名,&nbsp;成绩</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">FROM</span>&nbsp;XSB&nbsp;<span style="color: black;">JOIN</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;CJB&nbsp;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">ON</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;XSB.学号&nbsp;=&nbsp;CJB.学号&nbsp;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">WHERE</span>课程号&nbsp;=<span style="color: black;">206</span>&nbsp;&nbsp;<span style="color: black;">AND</span>&nbsp;成绩&gt;=<span style="color: black;">80</span>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">4.自连接</span></h3>
    <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>自连接。<span style="color: black;">运用</span>自连接时需为表指定两个别名,且对所有列的引用均要用别名限定。</p><span style="color: black;">SELECT</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;a.学号,&nbsp;a.课程号,&nbsp;b.课程号,&nbsp;a.成绩</p>&nbsp;<span style="color: black;">FROM</span>&nbsp;CJB&nbsp;a&nbsp;&nbsp;<span style="color: black;">JOIN</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;CJB&nbsp;b&nbsp;</p>&nbsp;&nbsp;&nbsp;<span style="color: black;">ON</span>&nbsp;&nbsp;a.成绩=b.成绩&nbsp;<span style="color: black;">AND</span>&nbsp;&nbsp;a.学号=b.学号&nbsp;<span style="color: black;">AND</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;a.课程号!=b.课程号</p><img src="https://mmbiz.qpic.cn/mmbiz_png/JfTPiahTHJhpGQtp8STJBAunM9pUXs5CWDibYSsHeic3AGgYrP8TPcfHU4xnG0kpE8ia0dbv3kmpicuRXQpzhufmqsA/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">结果<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">5.外连接(左外连接,右外连接、全外连接)</span></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">指定了OUTER关键字的为外连接,外连接的结果表<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>以下三种:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">左外连接(LEFT OUTER JOIN):</strong>结果表中除了<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>,以及<span style="color: black;">她们</span>选修的课程号,若学生未选修任何课,<span style="color: black;">亦</span>要<span style="color: black;">包含</span>其<span style="color: black;">状况</span></p>&nbsp;<span style="color: black;">SELECT</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;XSB.*&nbsp;,&nbsp;课程号</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">FROM</span>&nbsp;&nbsp;XSB&nbsp;&nbsp;<span style="color: black;">LEFT</span>&nbsp;<span style="color: black;">OUTER</span>&nbsp;<span style="color: black;">JOIN</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;CJB&nbsp;</p>&nbsp;&nbsp;&nbsp;<span style="color: black;">ON</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;XSB.学号&nbsp;=&nbsp;CJB.学号</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本例执行时,若有学生未选任何课程,则结果表中相应行的课程号字段值为NULL。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">右外连接(RIGHT OUTER JOIN):</strong>结果表中除了<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;"><strong style="color: blue;">完全外连接(FULL OUTER JOIN):</strong>结果表中除了<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;">其中的OUTER关键字均可省略</span>**</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">6.交叉连接(又名笛卡尔积)</span></h3>
    <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;">有两个集合A和B,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 =&nbsp;{(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 =&nbsp;{(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>两个集合相乘,不满足交换率,既 A×B ≠ B×A;A集合和B集合相乘,<span style="color: black;">包括</span>了集合A中元素和集合B中元素相结合的所有的可能性,即两个集合相乘得到的新集合的元素个数是 A集合的元素个数 × B集合的元素个数。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">交叉连接:</p><span style="color: black;">SELECT</span>&nbsp;*&nbsp;<span style="color: black;">from</span>&nbsp;表<span style="color: black;">1</span>&nbsp;<span style="color: black;">JOIN</span>&nbsp;表<span style="color: black;">2</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;">第1</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>,<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>时,应该<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><img src="https://mmbiz.qpic.cn/mmbiz_png/b96CibCt70iabxGraSfbk6yTjATjwMNXW8ibsPbATicsud3NfaS24FNreYJxJ77DPw6ibbMopSJcW3ys8Ga6HrSkAIA/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz/cZV2hRpuAPiaJQXWGyC9wrUzIicibgXayrgibTYarT3A1yzttbtaO0JlV21wMqroGYT3QtPq2C7HMYsvicSB2p7dTBg/640?wx_fmt=gif&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">有热门<span style="color: black;">举荐</span></span><span style="color: black;">

qxdl12345 发表于 2024-9-5 01:20:55

软文发布论坛开幕式圆满成功。 http://www.fok120.com

qzmjef 发表于 2024-10-5 17:49:13

“沙发”(SF,第一个回帖的人)‌

7wu1wm0 发表于 2024-10-8 18:09:24

请问、你好、求解、谁知道等。

wrjc1hod 发表于 2024-10-25 15:59:25

我赞同你的看法,你的智慧让人佩服,谢谢分享。
页: [1]
查看完整版本: 内连接、外连接、左连接、右连接、等值连接、自然连接和自连接之间的区别,看这篇就够了!