深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接
<div style="color: black; text-align: left; margin-bottom: 10px;">
<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>。 </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>表共有的列的值匹配两个表中的行。例如,检索 students和courses表中学生标识号相同的所有行。 </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>或完整<span style="color: black;">外边</span>联接。 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在 FROM子句中指定外联接时,<span style="color: black;">能够</span>由下列几组关键字中的一组指定:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1)LEFT JOIN或LEFT OUTER JOIN </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">左向外联接的结果集<span style="color: black;">包含</span> LEFT 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;">2)RIGHT JOIN 或 RIGHT OUTER JOIN</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;">3)FULL JOIN 或 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>联接返回左表和右表中的所有行。当某行在另一个表中<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>
<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;">联接</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;">FROM 子句中的表或视图可<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>信息,请参见<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;">例子: </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;"> a表 id name b表 id job parent_id </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 1 张3 1 23 1 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 2 李四 2 34 2 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 3 王武 3 34 4 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a.id同parent_id 存在关系</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;"> 1) 内连接 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> select a.*,b.* from a inner join b on a.id=b.parent_id </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;"> 1 张3 1 23 1 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 2 李四 2 34 2 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2)左连接</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> select a.*,b.* from a left join b on a.id=b.parent_id </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;"> 1 张3 1 23 1 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 2 李四 2 34 2 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 3 王武 null </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;">select a.*,b.* from a right join b on a.id=b.parent_id</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;"> 1 张3 1 23 1 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 2 李四 2 34 2 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> null 3 34 4 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 4) 完全连接 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> select a.*,b.* from a full join b on a.id=b.parent_id </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;"> 1 张3 1 23 1 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 2 李四 2 34 2 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> null 3 34 4 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 3 王武 null</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;">1、</span>交叉连接(CROSS JOIN)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">交叉连接(CROSS JOIN):有两种,显式的和隐式的,不带ON子句,返回的是两表的乘积,<span style="color: black;">亦</span>叫笛卡尔积。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如:下面的语句1和语句2的结果是相同的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句1:隐式的交叉连接,<span style="color: black;">无</span>CROSS JOIN。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID, O.ORDER_NUMBER, C.ID, C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O , CUSTOMERS C</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WHERE O.ID=1;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句2:显式的交叉连接,<span style="color: black;">运用</span>CROSS JOIN。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,C.ID,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O CROSS JOIN CUSTOMERS C</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WHERE O.ID=1;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句1和语句2的结果是相同的,<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;">2、</span>内连接(INNER JOIN)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">内连接(INNER JOIN):有两种,显式的和隐式的,返回连接表中符合连接<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>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如:下面的语句3和语句4的结果是相同的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句3:隐式的内连接,<span style="color: black;">无</span>INNER JOIN,形成的中间表为两个表的笛卡尔积。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM CUSTOMERS C,ORDERS O</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WHERE C.ID=O.CUSTOMER_ID;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句4:<span style="color: black;">表示</span>的内连接,<span style="color: black;">通常</span><span style="color: black;">叫作</span>为内连接,有INNER JOIN,形成的中间表为两个表经过ON<span style="color: black;">要求</span>过滤后的笛卡尔积。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM CUSTOMERS C INNER JOIN ORDERS O ON C.ID=O.CUSTOMER_ID;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句3和语句4的<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;">3、</span>外连接(OUTER JOIN):外连<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>行。外连接分三类:左外连接(LEFT OUTER JOIN)、右外连接(RIGHT OUTER JOIN)和全外连接(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>点是都返回符合连接<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;">左外连接还返回左表中不符合连接<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>的数据行。</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>是上左外连接和右外连接的数学合集(去掉重复),即“全外=左外 UNION 右外”。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">说明:左表<span style="color: black;">便是</span>在“(LEFT OUTER JOIN)”关键字左边的表。右表当然<span style="color: black;">便是</span>右边的了。在三种类型的外连接中,OUTER 关键字是可省略的。</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;">语句5:左外连接(LEFT OUTER JOIN)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O LEFT OUTER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句6:右外连接(RIGHT OUTER JOIN)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O RIGHT OUTER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>:WHERE<span style="color: black;">要求</span>放在ON后面<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;">语句7:WHERE<span style="color: black;">要求</span>独立。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O LEFT OUTER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WHERE O.ORDER_NUMBER<>MIKE_ORDER001;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句8:将语句7中的WHERE<span style="color: black;">要求</span>放到ON后面。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O LEFT OUTER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID AND O.ORDER_NUMBER<>MIKE_ORDER001;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">从语句7和语句8<span style="color: black;">查找</span>的结果来看,显然是不相同的,语句8<span style="color: black;">表示</span>的结果是难以理解的。<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>都写到WHERE子句中。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句9:全外连接(FULL OUTER JOIN)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O FULL OUTER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID;</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>给出的写法适合Oracle和DB2。<span style="color: black;">然则</span><span style="color: black;">能够</span><span style="color: black;">经过</span>左外和右外求合集来获取全外连接的<span style="color: black;">查找</span>结果。下图是上面SQL在Oracle下执行的结果:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句10:左外和右外的合集,<span style="color: black;">实质</span>上<span style="color: black;">查找</span>结果和语句9是相同的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O LEFT OUTER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">UNION</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O RIGHT OUTER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句9和语句10的<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;">4、</span>联合连接(UNION JOIN):这是一种很少见的连接方式。Oracle、MySQL均不支持,其<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;">语句11:联合<span style="color: black;">查找</span>(UNION JOIN)例句,还<span style="color: black;">无</span>找到能执行的SQL环境。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O UNION JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句12:语句11在DB2下的等价实现。还不<span style="color: black;">晓得</span>DB2<span style="color: black;">是不是</span>支持语句11呢!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O FULL OUTER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EXCEPT</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O INNER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句13:语句11在Oracle下的等价实现。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O FULL OUTER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MINUS</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O INNER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID;</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;"><span style="color: black;">5、</span>自然连接(NATURAL INNER JOIN):说真的,这种连接<span style="color: black;">查找</span><span style="color: black;">无</span>存在的价值,既然是SQL2标准中定义的,就给出个例子<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>中<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>ON语句,不<span style="color: black;">准许</span>指定<span style="color: black;">表示</span>列,<span style="color: black;">表示</span>列只能用*<span style="color: black;">暗示</span>(ORACLE环境下测试的)。<span style="color: black;">针对</span>每种连接类型(除了交叉连接外),均可指定NATURAL。下面给出几个例子。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句14:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT *</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O NATURAL INNER JOIN CUSTOMERS C;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句15:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT *</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O NATURAL LEFT OUTER JOIN CUSTOMERS C;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句16:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT *</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O NATURAL RIGHT OUTER JOIN CUSTOMERS C;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句17:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT *</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM ORDERS O NATURAL FULL OUTER JOIN CUSTOMERS C;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">6、</span>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;">第<span style="color: black;">1、</span>单表<span style="color: black;">查找</span>:<span style="color: black;">按照</span>WHERE<span style="color: black;">要求</span>过滤表中的记录,形成中间表(这个中间表对用户是不可见的);<span style="color: black;">而后</span><span style="color: black;">按照</span>SELECT的<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;">2、</span>两表连接<span style="color: black;">查找</span>:对两表求积(笛卡尔积)并用ON<span style="color: black;">要求</span>和连接连接类型进行过滤形成中间表;<span style="color: black;">而后</span><span style="color: black;">按照</span>WHERE<span style="color: black;">要求</span>过滤中间表的记录,并<span style="color: black;">按照</span>SELECT指定的列返回<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;">3、</span>多表连接<span style="color: black;">查找</span>:先对<span style="color: black;">第1</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>WHERE<span style="color: black;">要求</span>过滤中间表的记录,并<span style="color: black;">按照</span>SELECT指定的列返回<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>的过程是进行SQL优化的理论依据。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">7、</span>ON后面的<span style="color: black;">要求</span>(ON<span style="color: black;">要求</span>)和WHERE<span style="color: black;">要求</span>的区别:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ON<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;">WHERE<span style="color: black;">要求</span>:在有ON<span style="color: black;">要求</span>的SELECT语句中是过滤中间表的约束<span style="color: black;">要求</span>。在<span style="color: black;">无</span>ON的单表<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;">从<span style="color: black;">这儿</span><span style="color: black;">能够</span>看出,将WHERE<span style="color: black;">要求</span>移入ON后面是不恰当的。<span style="color: black;">举荐</span>的做法是:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ON只进行连接操作,WHERE只过滤中间表的记录。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">8、</span>总结</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><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><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>列相等的数据用内连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2、 Col_L是Col_R的子集时用右外连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3、 Col_R是Col_L的子集时用左外连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4、 Col_R和Col_L彼此有交集但彼此互不为子集时候用全外。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5、 求差操作的时候用联合<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>写到一块。例如:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT T1.C1,T2.CX,T3.CY</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM TAB1 T1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> INNER JOIN TAB2 T2 ON (T1.C1=T2.C2)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> INNER JOIN TAB3 T3 ON (T1.C1=T2.C3)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> LEFT OUTER JOIN TAB4 ON(T2.C2=T3.C3);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WHERE T1.X >T3.Y;</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/06ea733452414b208cff616aec577cbe~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723339528&x-signature=3HB4ocxxjcMbgZPdolSNDKnECjM%3D" style="width: 50%; margin-bottom: 20px;"></div>
</div>
你的努力一定会被看见,相信自己,加油。 我完全同意你的看法,期待我们能深入探讨这个问题。 谷歌外链发布 http://www.fok120.com/ 说得好啊!我在外链论坛打滚这么多年,所谓阅人无数,就算没有见过猪走路,也总明白猪肉是啥味道的。 认真阅读了楼主的帖子,非常有益。 请问、你好、求解、谁知道等。
页:
[1]