工作表查询中,内连接Inner join的讲解
<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;"><span style="color: black;">大众</span>好,<span style="color: black;">咱们</span>继续讲解VBA数据库<span style="color: black;">处理</span><span style="color: black;">方法</span>,今日讲解第56讲内容: 数据表<span style="color: black;">查找</span>中,内连接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>才讲解这块内容呢?是的,<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>SQL语句是操作数据库的一个非常重要的工具,我的观点<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>操作及其特点。</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>比较运算符比较被连接列的列值。在<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>的数据提取出来,<span style="color: black;">然则</span>型号,生产厂,数量来自"数据"工作表,而供应商的数据来自"数据2"的数据。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/75ec278f8eb44aca867bc6c5f68542c0~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347720&x-signature=MeXtycwEsBAevI98KtvdJUOu2LM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<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;">Sub mynzRecords_56() 第56讲</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Dim cnADO, rsADO As Object</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Dim strPath, strSQL As String</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Worksheets("56").Select</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Cells.ClearContents</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Set cnADO = CreateObject("ADODB.Connection")</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Set rsADO = CreateObject("ADODB.Recordset")</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">strPath = ThisWorkbook.FullName</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">cnADO.Open "provider=Microsoft.ACE.OLEDB.12.0;extended properties=excel 12.0;hdr=yes;imex=1;data source=" & strPath</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">strSQL = "Select a.型号,a.生产厂,a.数量,b.供应商 From [数据$]as a,[数据2$] as b Where a.型号=b.型号"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">strSQL = "Select a.型号,a.生产厂,a.数量,b.供应商 From [数据$] as a INNER JOIN [数据2$] as b ON a.型号=b.型号"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">rsADO.Open strSQL, cnADO, 1, 3</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">For i = 1 To rsADO.Fields.Count</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Cells(1, i) = rsADO.Fields(i - 1).Name</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Next</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Range("a2").CopyFromRecordset rsADO</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">rsADO.Close</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">cnADO.Close</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Set rsADO = Nothing</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Set cnADO = Nothing</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">End Sub</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码截图:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/4b763994c6e74ff7b0a8536e862a44da~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347720&x-signature=4i1NRHK06Q%2FMRq2tA62dGFguPB0%3D" style="width: 50%; margin-bottom: 20px;"></div>
<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 cnADO.Open "provider=Microsoft.ACE.OLEDB.12.0;extended properties=excel 12.0;hdr=yes;imex=1;data source=" & strPath</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">此语句的代码<span style="color: black;">创立</span>起ADO和Excel的连接,<span style="color: black;">大众</span><span style="color: black;">能够</span>直接利用,只要是连接07版本以上的EXCEL<span style="color: black;">就可</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 strSQL = "Select a.型号,a.生产厂,a.数量,b.供应商 From [数据$]as a,[数据2$] as b Where a.型号=b.型号"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">此语句的代码为一个典型的SQL内连接的语句,用的是WHERE的表达式,<span style="color: black;">大众</span>要<span style="color: black;">重视</span>我字段的写法和定义a,b 两个数据表的写法。不<span style="color: black;">能够</span>有丝毫的错误。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 strSQL = "Select a.型号,a.生产厂,a.数量,b.供应商 From [数据$] as a INNER JOIN [数据2$] as b ON a.型号=b.型号"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">此语句的代码为一个典型的SQL内连接的语句的另一种写法,用的是的INNER JOIN….on…的表达式,<span style="color: black;">大众</span>要<span style="color: black;">重视</span>这种写法和定义a,b 两个数据表的写法。大体上和上面的写法是一致的,读者<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>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/8e68d7345c9d44c8a0fbd712d5654680~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347720&x-signature=F8%2F%2BzlF4nn0vgDi8uY%2FvvFKeyFA%3D" style="width: 50%; margin-bottom: 20px;"></div>
<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>型号为QQ01的供应商为RRR05是取自数据2的数据。</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;">2 内连接的两种表法方式是什么?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 <span style="color: black;">倘若</span>将select后的字段去掉换成*号会是什么样的结果?</p>
</div>
你的言辞如同繁星闪烁,点亮了我心中的夜空。 你的见解独到,让我受益匪浅,非常感谢。 谷歌网站排名优化 http://www.fok120.com/ 你的言辞如同繁星闪烁,点亮了我心中的夜空。 你字句如珍珠,我珍藏这份情。 我们有着相似的经历,你的感受我深有体会。 “板凳”(第三个回帖的人) 楼主继续加油啊!外链论坛加油! seo常来的论坛,希望我的网站快点收录。
页:
[1]
2