怎么样在两个区别的SQL中创立内连接
<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>好,今日继续讲解VBA数据库<span style="color: black;">处理</span><span style="color: black;">方法</span>,今日的内容是第67讲:内连接INNER<span style="color: black;">怎样</span>在两个<span style="color: black;">区别</span>的SQL中<span style="color: black;">创立</span>连接。今日的内容是之前内容的延伸,在前面的讲解中我讲了利用INNER连接的两个数据源都是字段值与值的<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>变成值的<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语句。</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>报价是:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/c63599f33edc42f3aae95f54c755f7a0~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347425&x-signature=V2BZ44wMoGLomtJ630I7cgyTPLg%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>类别<span style="color: black;">必须</span>的人数和价格如上,<span style="color: black;">同期</span>后勤<span style="color: black;">分部</span>的报价如下:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/60447fe5bcb44833a6a8f7c4ba431bc7~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347425&x-signature=tF7nk9jD5m6nSm%2Bmjub6nP4sInc%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>的工具套数和单价。</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;">思路:<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;">Sub mynzRecords_67() 第67讲 内连接Inner join 连接两个SQL</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("67").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;">strSQL1 = "select 项目,SUM(人数) AS 总人数,SUM(价格) AS 总价格 from [数据4$] group by 项目"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">strSQL2 = "select 项目,SUM(车辆) AS 出动车辆,SUM(工具套数) AS 工具总套数,SUM(工具套数*工具单价) AS " _</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">& "工具总价格 from [数据5$] group by 项目"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">strSQL = "Select a.项目,a.总人数,a.总价格,b.出动车辆,b.工具总套数,b.工具总价格 From (" _</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">& strSQL1 & ") a Inner Join (" & strSQL2 & ") b " _</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">& "ON a.项目=b.项目 GROUP BY a.项目,a.总人数,a.总价格,b.出动车辆,b.工具总套数,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/49ab8e48fd5c469583aec45e2fc09c34~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347425&x-signature=yOJyXDsbO00F92thhUNTmGUMsgI%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>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1 <span style="color: black;">以上</span>代码先用SQL1 和SQL2 对"数据4" 工作表和"数据5"工作表进行数据的汇总。<span style="color: black;">创立</span>项目的汇总数据。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 strSQL1 = "select 项目,SUM(人数) AS 总人数,SUM(价格) AS 总价格 from [数据4$] group by 项目"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上</span>SQL语句实现的的是对工作表4的汇总,上面有新的字段的设置。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 strSQL2 = "select 项目,SUM(车辆) AS 出动车辆,SUM(工具套数) AS 工具总套数,SUM(工具套数*工具单价) AS " _</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">& "工具总价格 from [数据5$] group by 项目"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上</span>SQL语句实现的的是对工作表5的汇总,上面<span style="color: black;">亦</span>有新的字段的设置。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4 strSQL = "Select a.项目,a.总人数,a.总价格,b.出动车辆,b.工具总套数,b.工具总价格 From (" _</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">& strSQL1 & ") a Inner Join (" & strSQL2 & ") b " _</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">& "ON a.项目=b.项目 GROUP BY a.项目,a.总人数,a.总价格,b.出动车辆,b.工具总套数,b.工具总价格"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">利用<span style="color: black;">以上</span>的SQL语句,实现了对SQL1和SQL2的内连接,<span style="color: black;">要求</span>是a.项目=b.项目。</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>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面看本实例的运行<span style="color: black;">状况</span>:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/3f990692ee2b4234a40f95a618cfe1da~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347425&x-signature=V4EMtaGbbbaI92NJqbza700RtTE%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>。</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>连接于两个SQL的内间接<span style="color: black;">查找</span>?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 应用于连接于两个SQL的内间接<span style="color: black;">查找</span>是什么样的<span style="color: black;">实质</span>场景?</p>
</div>
交流如星光璀璨,点亮思想夜空。 认真阅读了楼主的帖子,非常有益。 网站建设seio论坛http://www.fok120.com/ 论坛的成功是建立在我们诚恳、务实、高效、创新和团结合作基础上,我们要把这种精神传递下去。
页:
[1]