sql平常四种连接查询
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/y8O9gpPSonDXjGJ9Lib4ILn5R63qZiajRdPmhlMwn6gC5V6sxWUXqF3eYF70jKhmEt5OIAcgzplD6XVoYS7HOmEQ/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><strong style="color: blue;"><span style="color: black;">1、内联接</span></strong></span>
<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;"><span style="color: black;">包含</span>相等联接和自然联接。</span></p><span style="color: black;">
<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>
</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;">2、外联接。</span></strong></span>
<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></p><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在 FROM子句中指定外联接时,<span style="color: black;">能够</span>由下列几组关键字中的一组指定:</p><strong style="color: blue;">1)LEFT JOIN或LEFT OUTER JOIN </strong>左向外联接的结果集<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>列表列均为空值。
</span><span style="color: black;"><strong style="color: blue;">2)RIGHT JOIN 或 RIGHT OUTER JOIN</strong>右向外联接是左向外联接的反向联接。将返回右表的所有行。<span style="color: black;">倘若</span>右表的某行在左表中<span style="color: black;">无</span>匹配行,则将为左表返回空值。 </span><span style="color: black;"><strong style="color: blue;">3)FULL JOIN 或 FULL OUTER JOIN</strong>
<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>
</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;">3、交叉联接 </span></strong></span>
<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 style="color: black;">叫作</span>作笛卡尔积。</span></p><span style="color: black;">
<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>外联接。</p>
</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">1、</span>交叉连接(CROSS JOIN)</span></strong></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">交叉连接(CROSS JOIN):</span><span style="color: black;">有两种,显式的和隐式的,不带ON子句,返回的是两表的乘积,<span style="color: black;">亦</span>叫笛卡尔积。</span></p><span style="color: black;">例如:下面的语句1和语句2的结果是相同的。</span><strong style="color: blue;"><span style="color: black;">语句1:隐式的交叉连接,<span style="color: black;">无</span>CROSS JOIN。</span></strong><span style="color: black;">
<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>WHERE O.ID=1;
</span><strong style="color: blue;"><span style="color: black;">语句2:显式的交叉连接,<span style="color: black;">运用</span>CROSS JOIN。</span></strong><span style="color: black;">
<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>语句1和语句2的结果是相同的,<span style="color: black;">查找</span>结果如下:
</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">2、</span>内连接(INNER JOIN)</span></strong></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">内连接(INNER JOIN):</span><span style="color: black;">有两种,显式的和隐式的,返回连接表中符合连接<span style="color: black;">要求</span>和<span style="color: black;">查找</span><span style="color: black;">要求</span>的数据行。</span><span style="color: black;">(<span style="color: black;">所说</span>的链接表<span style="color: black;">便是</span>数据库在做<span style="color: black;">查找</span>形成的中间表)。</span></p><span style="color: black;">例如:下面的语句3和语句4的结果是相同的。</span><strong style="color: blue;"><span style="color: black;">语句3:隐式的内连接,<span style="color: black;">无</span>INNER JOIN,形成的中间表为两个表的笛卡尔积。</span></strong><span style="color: black;">
<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>WHERE C.ID=O.CUSTOMER_ID;
</span><strong style="color: blue;"><span style="color: black;">语句4:<span style="color: black;">表示</span>的内连接,<span style="color: black;">通常</span><span style="color: black;">叫作</span>为内连接,有INNER JOIN,形成的中间表为两个表经过ON<span style="color: black;">要求</span>过滤后的笛卡尔积。</span></strong><span style="color: black;">
<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>语句3和语句4的<span style="color: black;">查找</span>结果:
</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">3、</span>外连接(OUTER JOIN)</span></strong></span><span style="color: black;">外</span><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><span style="color: black;">外连接分三类:</span><span style="color: black;">左外连接(LEFT OUTER JOIN)、右外连接(RIGHT OUTER JOIN)和全外连接(FULL OUTER JOIN)。</span><span style="color: black;">
<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>说明:左表<span style="color: black;">便是</span>在“(LEFT OUTER JOIN)”关键字左边的表。右表当然<span style="color: black;">便是</span>右边的了。在三种类型的外连接中,OUTER 关键字是可省略的。
</span><span style="color: black;">
<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>
</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">4、</span>联合连接(UNION JOIN)</span></strong></span>
<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;">Oracle、MySQL均不支持,其<span style="color: black;">功效</span>是:</span><span style="color: black;">找出全外连接和内连接之间差异的所有行。</span><span style="color: black;">这在数据分析中排错中比较常用。</span><span style="color: black;"><span style="color: black;">亦</span><span style="color: black;">能够</span>利用数据库的集合操作来实现此功能。</span></p><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">5、</span>自然连接(NATURAL INNER JOIN)</span></strong></span>
<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></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">自然连接无需指定连接列,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><span style="color: black;">不<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><span style="color: black;"><span style="color: black;">针对</span>每种连接类型(除了交叉连接外),均可指定NATURAL。</span></p><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">6、</span>SQL<span style="color: black;">查找</span>的基本原理</span></strong></span>
<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></p><span style="color: black;"><strong style="color: blue;">第<span style="color: black;">1、</span>单表<span style="color: black;">查找</span>:</strong>
<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>过滤表中的记录,形成中间表(这个中间表对用户是不可见的);<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><strong style="color: blue;">第<span style="color: black;">2、</span>两表连接<span style="color: black;">查找</span></strong>
<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><span style="color: black;">按照</span>WHERE<span style="color: black;">要求</span>过滤中间表的记录,并<span style="color: black;">按照</span>SELECT指定的列返回<span style="color: black;">查找</span>结果。</p><strong style="color: blue;">第<span style="color: black;">3、</span>多表连接<span style="color: black;">查找</span></strong>
<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>,<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>理解SQL<span style="color: black;">查找</span>的过程是进行SQL优化的理论依据。
</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">7、</span>ON后面的<span style="color: black;">要求</span>(ON<span style="color: black;">要求</span>)和WHERE<span style="color: black;">要求</span>的区别</span></strong></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">ON<span style="color: black;">要求</span></span></strong><span style="color: black;">:</span><span style="color: black;">是过滤两个链接表笛卡尔积形成中间表的约束<span style="color: black;">要求</span>。</span></p><strong style="color: blue;"><span style="color: black;">WHERE<span style="color: black;">要求</span>:</span></strong><span style="color: black;">在有ON<span style="color: black;">要求</span>的SELECT语句中是过滤中间表的约束<span style="color: black;">要求</span>。</span><span style="color: black;">在<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>中间表的返回结果的约束。</span><span style="color: black;">
<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>ON只进行连接操作,WHERE只过滤中间表的记录。
</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">8、</span>总结</span></strong></span><span style="color: black;">
<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>写到<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>WHERE T1.X >T3.Y;
</span><strong style="color: blue;"><span style="color: black;"> 添加小唐老师<span style="color: black;">微X</span>免费领取 </span></strong><strong style="color: blue;"><span style="color: black;">【Python入门视频一份】</span></strong>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/y8O9gpPSonCdrmmPJ4CFr5ib4FDyltvu00D5YnmL2iahEf5ZCU6ibVHgziaFks77OsibWCPibzTzzrickgymklAicUicBLw/640?wx_fmt=jpeg&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;"><a style="color: black;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/y8O9gpPSonBz3iaLKrycnGt15xtTqla5mwCRl9hPFdFfRMsnicCS8cvOSUe4Wuta8vruDqBl1icW8ZYhSTIyY3JdQ/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/y8O9gpPSonBz3iaLKrycnGt15xtTqla5mRDmXTiaNN6fHCibZvGZXum8U14dBbLWUbyt0lSlzvAWUlYib9XVDGTwmA/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/y8O9gpPSonBz3iaLKrycnGt15xtTqla5mFZcqeOKj0sFvJpaLh0SahhhYfpKJBZCZ8ic6qOLFov6Dw1IuI5iao8CQ/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/y8O9gpPSonBz3iaLKrycnGt15xtTqla5mHdYqK7nbNGeBmfMzVc1DKbboWYYhPT87RVnrWRf0GficPNvwIYAr9mA/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></a></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/y8O9gpPSonBz3iaLKrycnGt15xtTqla5mMIfrqAJPKFrXDl1EwHHTJaFwicgb1X2geCI3WVnBZaKh5iad078pY8ww/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
我赞同你的看法,你的智慧让人佩服,谢谢分享。
页:
[1]