内连接、外连接、左连接、右连接、等值连接、自然连接和自连接之间的区别
<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>:</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>是<、<=、=、>、>=、!=、<>、!< 和 !>,当比较符为“=”时,<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.* , CJB.*</p> <span style="color: black;">FROM</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> XSB , CJB</p> <span style="color: black;">WHERE</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">XSB.学号 = 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;"> XSB.* , CJB.课程号, CJB.成绩</p><span style="color: black;">FROM</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> XSB , CJB</p><span style="color: black;">WHERE</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">XSB.学号= 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;"> *</p> <span style="color: black;">FROM</span> XSB <span style="color: black;">INNER</span> <span style="color: black;">JOIN</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> CJB </p> <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> XSB.学号 =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;"> 姓名, 成绩</p> <span style="color: black;">FROM</span> XSB <span style="color: black;">JOIN</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> CJB </p> <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> XSB.学号 = CJB.学号 </p> <span style="color: black;">WHERE</span> 课程号 = <span style="color: black;">206</span> <span style="color: black;">AND</span> 成绩>=<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;"> a.学号, a.课程号, b.课程号, a.成绩</p> <span style="color: black;">FROM</span> CJB a <span style="color: black;">JOIN</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> CJB b </p> <span style="color: black;">ON</span> a.成绩=b.成绩 <span style="color: black;">AND</span>a.学号=b.学号<span style="color: black;">AND</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> a.课程号!=b.课程号</p><img src="https://mmbiz.qpic.cn/mmbiz_png/JfTPiahTHJhpGQtp8STJBAunM9pUXs5CWDibYSsHeic3AGgYrP8TPcfHU4xnG0kpE8ia0dbv3kmpicuRXQpzhufmqsA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&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> <span style="color: black;">SELECT</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> XSB.* , 课程号</p> <span style="color: black;">FROM</span> XSB <span style="color: black;">LEFT</span> <span style="color: black;">OUTER</span> <span style="color: black;">JOIN</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> CJB </p> <span style="color: black;">ON</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> XSB.学号 = 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 = {(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>两个集合相乘,不满足交换率,既 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>*<span style="color: black;">from</span> 表<span style="color: black;">1</span> <span style="color: black;">JOIN</span> 表<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>都会使用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>
<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 style="color: black;">爱好</span>的<span style="color: black;">伴侣</span>欢迎</span></strong><strong style="color: blue;"><span style="color: black;">关注,订阅<span style="color: black;">更加多</span>精彩内容</span></strong></p>
软文发布平台 http://www.fok120.com/ i免费外链发布平台 http://www.fok120.com/
页:
[1]