6257rv7 发表于 2024-8-4 15:07:01

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>所选的行。 &nbsp; &nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">联接可分为以下几类: &nbsp; &nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1、内联接(典型的联接运算,<span style="color: black;">运用</span>像 = &nbsp;或 &lt;&gt; 之类的比较运算符)。<span style="color: black;">包含</span>相等联接和自然联接。 &nbsp; &nbsp;&nbsp;</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>联接。 &nbsp; &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在 FROM子句中指定外联接时,<span style="color: black;">能够</span>由下列几组关键字中的一组指定: &nbsp; &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1)LEFT &nbsp;JOIN或LEFT OUTER JOIN &nbsp; &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">左向外联接的结果集<span style="color: black;">包含</span> &nbsp;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 &nbsp;JOIN 或 RIGHT &nbsp;OUTER &nbsp;JOIN &nbsp; &nbsp;&nbsp;</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 &nbsp;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>基表的数据值。 &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3、交叉联接 &nbsp;&nbsp;</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>外联接。 &nbsp; &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例子: &nbsp;&nbsp;</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;">&nbsp; a表 &nbsp; &nbsp; id &nbsp; name &nbsp; &nbsp; <span style="color: black;">b表 &nbsp; &nbsp; id &nbsp; job &nbsp; parent_id &nbsp;&nbsp;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; 张3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&nbsp; 1 &nbsp; &nbsp; 23 &nbsp; &nbsp; 1 &nbsp;&nbsp;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; 李四 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: black;"> 2 &nbsp; &nbsp; 34 &nbsp; &nbsp; 2 &nbsp;&nbsp;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; 王武 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: black;"> 3 &nbsp; &nbsp; 34 &nbsp; &nbsp; 4 &nbsp; &nbsp; </span>&nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a.id &nbsp; 同 &nbsp; &nbsp;parent_id &nbsp; 存在关系</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&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;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&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;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;">-------------------------------------------------- &nbsp; &nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">&nbsp;1) 内连接 &nbsp;&nbsp;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; select &nbsp; a.*,b.* &nbsp; from &nbsp; a &nbsp; inner &nbsp; join &nbsp; b &nbsp; &nbsp; on &nbsp; a.id=b.parent_id; &nbsp; &nbsp; &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; 结果是 &nbsp; &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1 &nbsp; 张3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; 23 &nbsp; &nbsp; 1</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; 2 &nbsp; 李四 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2 &nbsp; &nbsp; 34 &nbsp; &nbsp; 2 &nbsp;&nbsp;</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&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">&nbsp; &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">&nbsp; 2)左连接 &nbsp;&nbsp;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; select &nbsp; a.*,b.* &nbsp; from &nbsp; a &nbsp; left &nbsp; join &nbsp; b &nbsp; &nbsp; on &nbsp; a.id=b.parent_id; &nbsp; &nbsp; &nbsp;&nbsp;</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;">&nbsp; 1 &nbsp; 张3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; 23 &nbsp; &nbsp; 1 &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; 2 &nbsp; 李四 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2 &nbsp; &nbsp; 34 &nbsp; &nbsp; 2 &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; 3 &nbsp; 王武 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;null &nbsp;&nbsp;</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&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;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;">&nbsp;3) 右连接 &nbsp;&nbsp;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">select &nbsp; a.*,b.* &nbsp; from &nbsp; a &nbsp; right &nbsp; join &nbsp; b &nbsp; &nbsp; on &nbsp; a.id=b.parent_id;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; 结果是 &nbsp; &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; 1 &nbsp; 张3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; 23 &nbsp; &nbsp; 1 &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; 2 &nbsp; 李四 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2 &nbsp; &nbsp; 34 &nbsp; &nbsp; 2 &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">null &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; &nbsp; 34 &nbsp; &nbsp; 4</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; &nbsp;&nbsp;<img src="http://mmbiz.qpic.cn/mmbiz/e8AxAEYfwM0DlvwiahV9tGwVkNE2IGqKoSQDJOu1MTv9j9mRHaGwjH9WHkx7DfP6VL04BhkBI0s39N064XnUHuQ/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>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">&nbsp;4) 完全连接 &nbsp;&nbsp;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; select &nbsp; a.*,b.* &nbsp; from &nbsp; a &nbsp; full &nbsp; join &nbsp; b &nbsp; &nbsp; on &nbsp; a.id=b.parent_id;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; 结果是 &nbsp; &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; 1 &nbsp; 张3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; 23 &nbsp; &nbsp; 1 &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 &nbsp; 李四 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; 34 &nbsp; &nbsp; 2</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; null &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;    &nbsp;3 &nbsp; &nbsp; 34 &nbsp; &nbsp; 4 &nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp; 3 &nbsp; 王武 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;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 &nbsp; a.*,b.* &nbsp; from &nbsp; a &nbsp; left &nbsp; join &nbsp; b &nbsp; &nbsp; on &nbsp; 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;"> &nbsp;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;">&nbsp; select &nbsp; a.*,b.* &nbsp; from &nbsp; a &nbsp; right &nbsp; join &nbsp; b &nbsp; &nbsp; on &nbsp; 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>




2557497911 发表于 2024-8-29 15:28:43

“BS”(鄙视的缩写)‌

b1gc8v 发表于 2024-10-1 17:57:03

回顾历史,我们感慨万千;放眼未来,我们信心百倍。

j8typz 发表于 6 天前

你的话语如春风拂面,让我感到无比温暖。
页: [1]
查看完整版本: Mysql的 4种表连接方式