Excel VBA+ADO+SQL入门教程016:多表连接查询(下)
<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;"><span style="color: black;">诸君好,咱们之前聊了交叉连接和内连接,今天来聊外连接,<span style="color: black;">亦</span><span style="color: black;">便是</span>OUT JOIN……</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;"><span style="color: black;">左外连接的关键字是LEFT OUT JOIN,它返回左表所有的记录行。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">右外连接的关键字是RIGHT OUT JOIN,它返回右表所有的记录行。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">全外连接的关键字是FULL OUT JOIN,它返回<span style="color: black;">上下</span>表所有的记录行——但Excel和ACCESS不支持该用法。<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></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/SbWgux809jWNsPlY7GHJZySUOibQtkAhpzxJtEv3IRVeoteTPv8icR8nh459rTboN7LjLwbXtzEW2u0tI5ph4GVw/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;">不管哪一种外连接,关键字OUT都是可省略的,<span style="color: black;">例如</span>LEFT OUT JOIN<span style="color: black;">能够</span>写成LEFT JOIN……</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>的,都是通过关键字ON指定连接<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;">2.</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;"><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></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/SbWgux809jWNsPlY7GHJZySUOibQtkAhp0HKpaia4NYiam3zSAf4ax6xzO9D5JR9yCuLDfcj9JHzuiagjMYlI50f6Q/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></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;">SELECT a.* , b.成绩 FROM [花名册$]a LEFT JOIN [成绩表$]b ON a.姓名=b.姓名</span></strong></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>是两表的姓名相等(ON a.姓名=b.姓名)。</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></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/SbWgux809jWNsPlY7GHJZySUOibQtkAhpH00LnrMMnMFyBkSvq65Om3Utia8VQ4oOog7hA46SicPQ6cvMJd87a7OQ/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>不管<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;"><span style="color: black;">倘若</span>某条记录行未得到匹配,对应的结果值会以null<span style="color: black;">暗示</span>,在Excel中<span style="color: black;">亦</span><span style="color: black;">便是</span>返回空白,例如传说中调皮的‘看见星光’童鞋,它的成绩即为NULL。</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;">运用</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;">当然<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;">咱们</span>只要将‘花名册’表放在连接位置的右边<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;">语句如下:</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;">SELECT b.*, a.成绩 FROM [成绩表$]a RIGHT JOIN [花名册$] b ONa.姓名=b.姓名</span></strong></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></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">问题2:<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;">……哒哒哒,时间过去20秒,坦白说这个问题和上个问题并<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;">亦</span><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;">左外连接:</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;">SELECT a.*, b.学号 FROM [成绩表$]a LEFT JOIN [花名册$] b ON a.姓名=b.姓名</span></strong></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;"><strong style="color: blue;"><span style="color: black;">SELECT b.*, a.学号 FROM [花名册$]a RIGHT JOIN [成绩表$] b ONa.姓名=b.姓名</span></strong></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></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/SbWgux809jWNsPlY7GHJZySUOibQtkAhp9VCwANLhg2wmKYsmAX48Ykl1vXPzORl0FwfDjyKuHic1z78UO3lnHAw/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 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;">能够</span>写成如下:</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;">SELECT * FROM [花名册$]a FULL JOIN [成绩表$] b ON a.姓名=b.姓名</span></strong></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>,之前说过,EXCEL并不支持全外连接的语句。</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></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;">经过</span>左外连接+右外连接UNION合并去重的方式来实现。</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;"><strong style="color: blue;"><span style="color: black;">SELECT * FROM [花名册$]a LEFT JOIN [成绩表$] b ON a.姓名=b.姓名 </span></strong></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;">UNION </span></strong></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;">SELECT * FROM [花名册$]a RIGHT JOIN [成绩表$] b ON a.姓名=b.姓名</span></strong></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>运算符是UNION,不是UNION ALL。它们之间的区别<span style="color: black;">咱们</span>在之前的章节<span style="color: black;">仔细</span>讲过了,</span><a style="color: black;"><span style="color: black;">Excel VBA+ADO+SQL入门教程012:UNION多表数据合并</span></a><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;">得到<span style="color: black;">查找</span>结果如下:</span></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/SbWgux809jWNsPlY7GHJZySUOibQtkAhpzevJuP6STWmTu6vkYn1ejAA6aH6ULo9z9KjG63pX2O9VfrfEhnoW7w/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>结果不是很理想,<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></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;">SELECT 学号,IIF(a.姓名 IS NULL,b.姓名,a.姓名) AS 姓名,成绩 FROM(SELECT * FROM [花名册$]a LEFT JOIN [成绩表$] b on a.姓名=b.姓名 UNION SELECT * FROM [花名册$]a RIGHT JOIN [成绩表$] b on a.姓名=b.姓名)</span></strong></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>中的FROM子句指定的表<span style="color: black;">源自</span>是之前UNON语句得出的中间表。<span style="color: black;">经过</span>IIF函数,系统判断a.姓名的记录<span style="color: black;">是不是</span>为NULL,<span style="color: black;">倘若</span>是NULL,则返回b.姓名,否则返回a.姓名。</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></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/SbWgux809jWNsPlY7GHJZySUOibQtkAhpTDJW2wZmsQPiccb1ibTZHwpQmL7cf2cSEgheqFxgKNnZjyFyqdBoXLqQ/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;">3.</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>SQL<span style="color: black;">查找</span>语句的多表连接部分,<span style="color: black;">咱们</span>先后分享了简单粗暴的FROM+WHERE的交叉连接、INNER JOIN的内连接以及LEFT JOIN左外连接和RIGHT JOIN右外连接等。</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;">另一</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></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;">OVER </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;"><span style="color: black;">图文制作:看见星光</span></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/BAbVqibwwtmxxiakyyy6k4j7fiabAZpuxDk0ML0w7nXF1icibXhURe0B87icW8ILAMcNUBCic01abs08AnmaPgPEZzAOA/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;">VBA编程与实践</span></p>
你的努力一定会被看见,相信自己,加油。 感谢您的精彩评论,为我带来了新的思考角度。 楼主继续加油啊!外链论坛加油!
页:
[1]