Mysql的 4种表连接方式
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以前读书学习的是SQL2000,<span style="color: black;">无</span>深入学习 过MySQL,今天给<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>的视图是FULL OUT 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;">联接<span style="color: black;">要求</span>可在FROM或WHERE子句中指定,<span style="color: black;">意见</span>在FROM子句中指定联接<span style="color: black;">要求</span>。WHERE和HAVING子句<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;">1、内联接(典型的联接运算,<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>表共有的列的值匹配两个表中的行。例如,检索 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>联接。 </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>作笛卡尔积。</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>外联接。 </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;">-------------------------------------------------</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> a表 id name <span style="color: black;">b表 id job parent_id </span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 1 张3 <span style="color: black;"> 1 23 1 </span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 2 李四 <span style="color: black;"> 2 34 2 </span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 3 王武 <span style="color: black;"> 3 34 4 </span> </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;"><img src="http://mmbiz.qpic.cn/mmbiz/e8AxAEYfwM0DlvwiahV9tGwVkNE2IGqKoVvKp5rMvo3rA2A8lmgtnP39DdP49U3nEyy3l9xKBlRdwhjG6Ne31XQ/640?wx_fmt=png&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;"><img src="http://mmbiz.qpic.cn/mmbiz/e8AxAEYfwM0DlvwiahV9tGwVkNE2IGqKoFSxVl2eeKn9sF9BR9RicovMZoIn0QhIibCRfaQljMIrDeibgXHudrMEtg/640?wx_fmt=png&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;">-------------------------------------------------- </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"> 1) 内连接 </span></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;"><img src="http://mmbiz.qpic.cn/mmbiz/e8AxAEYfwM0DlvwiahV9tGwVkNE2IGqKoB20jcjvF4tzq2y2MceicPicYeYHIsxRRv0KWgdnsQJ3gpfgXWkpYlyRQ/640?wx_fmt=png&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;"><span style="color: black;"> 2)左连接 </span></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;"><img src="http://mmbiz.qpic.cn/mmbiz/e8AxAEYfwM0DlvwiahV9tGwVkNE2IGqKodlcFaNzsBWgWS5YCexfaHB9aWaejb5Wx0ia2wiagGpvvicv6ibwHrzsGmQ/640?wx_fmt=png&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;"><span style="color: black;"> 3) 右连接 </span></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;"> <img src="http://mmbiz.qpic.cn/mmbiz/e8AxAEYfwM0DlvwiahV9tGwVkNE2IGqKoSQDJOu1MTv9j9mRHaGwjH9WHkx7DfP6VL04BhkBI0s39N064XnUHuQ/640?wx_fmt=png&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;"><span style="color: black;"> 4) 完全连接 </span></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;"><img src="http://mmbiz.qpic.cn/mmbiz/e8AxAEYfwM0DlvwiahV9tGwVkNE2IGqKoPmGKU92CA7WZbOWhq5PFwaibzsTUOSz9mXGnqicLIZcroicsSfk7ugIWA/640?wx_fmt=png&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;"><span style="color: black;"><span style="color: black;">重视</span>:<span style="color: black;">因为</span>mysql不支持全连接,<span style="color: black;">那样</span><span style="color: black;">必须</span>用<span style="color: black;">UNION 把<span style="color: black;">上下</span>连接联合起来<span style="color: black;">查找</span>,才<span style="color: black;">能够</span>得到结果</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 style="color: black;">select a.*,b.* from a left join b on a.id=b.parent_id</span></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 style="color: black;"><span style="color: black;"> UNION</span></span></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 style="color: black;"><span style="color: black;"> select a.*,b.* from a right join b on a.id=b.parent_id;</span></span></span></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>
“BS”(鄙视的缩写) 回顾历史,我们感慨万千;放眼未来,我们信心百倍。 你的话语如春风拂面,让我感到无比温暖。
页:
[1]