SQL 查询语句-WHERE
<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>SQL关键词WHERE<span style="color: black;">查找</span>中的<span style="color: black;">要求</span>指定要满足什么标准信息,去掉不满足<span style="color: black;">要求</span>的数据(删除用户不要的数据)。 WHERE语句中<span style="color: black;">能够</span>有多个<span style="color: black;">要求</span>,<span style="color: black;">要求</span>之间<span style="color: black;">能够</span>用操作符AND <span style="color: black;">或</span>OR进行连接。 WHERE语句的语法结构如下:</p>SELECT 列字段名<span style="color: black;">叫作</span> FROM [表名<span style="color: black;">叫作</span>$] WHERE 列字段名 运算符 值<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">运算符<span style="color: black;">包括</span><strong style="color: blue;">大于、<span style="color: black;">少于</span>、等于、不等于、大于或等于、<span style="color: black;">少于</span>或等于、IN、 BETWEEN、AND</strong>等 。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">应用实例说明,还是昨天数据(为了更好的更新今天的<span style="color: black;">文案</span>,修改了一点数据),只筛选出2号仓的数据;再筛选出入库数量大于60的数据</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">源数据:</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/pgc-image/8c5da02b0e4945c8a80eba0bb9e87f93~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347403&x-signature=1CaTbMobS%2F4pFEPkcgaCQghgeuo%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">查找</span>内容</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">只<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/c40dc773b26f44ba8595d968df3780f5~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347403&x-signature=SziNxNRrazr9DCrpy%2BbLXb8OC3Y%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>2号仓且入库数量大于60的数据:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/b400b54eddb1430c83bcfa1bfb674634~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347403&x-signature=VrT20MQRu4qxh%2FAnXdAoMtENqyM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">代码运行的结果如下:</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/5f7b8c516298444393a370e41cc6b389~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347403&x-signature=YmeZ8jwIe5%2BlSr39h4DNeDr4Np0%3D" style="width: 50%; margin-bottom: 20px;"></div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/fbd15f25779b40b1810dd715f2365a35~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347403&x-signature=Jqt%2Fja16k8Yzat6Bak%2F5C8pqtN4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">代码如下:</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/67aa73ecaf02449c97c5be74103b636e~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347403&x-signature=Ozsdbcf8d%2BiQGv4pjj5CXO0Epos%3D" style="width: 50%; margin-bottom: 20px;"></div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/b27bf34cdb564d0ab211423ccc7141f8~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347403&x-signature=V4zQY06NdggMJdszQjglL3A3ADk%3D" style="width: 50%; margin-bottom: 20px;"></div>Sub DoSql_Execute3()
Dim cnn As Object, rst As Object
Dim Mypath As String, Str_cnn As String, Sql As String
Dim i As Long
Set cnn = CreateObject("adodb.connection")
以上是<span style="color: black;">第1</span>步,后期绑定ADO
Mypath = ThisWorkbook.FullName
以上获取当前工作簿的路径及名<span style="color: black;">叫作</span>
If Application.Version < 12 Then
格式为.xls,调用下述语句
Str_cnn = "Provider=Microsoft.jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & Mypath
Else
格式为.xlsx,调用下述语句
Str_cnn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Mypath
End If
cnn.Open Str_cnn
判断一下当前Excel的版本,12.0以下<span style="color: black;">运用</span>“Provider=Microsoft.Jet.Oledb.4.0”,以上<span style="color: black;">运用</span>“Microsoft.ACE.OLEDB.12.0”
以上是第二步,<span style="color: black;">创立</span>链接
Sql = "SELECT 条码,仓位,货号,入库数量 FROM [商品信息目录$] where 仓位=2号仓 and 入库数量>60"
Sql语句,<span style="color: black;">查找</span>商品信息目录表中条码,仓位,货号,入库数量 并且只筛选出仓位=2号仓的数据,并且入库的数量要大于60
.ClearContents
清空区域内容
Range("a2").CopyFromRecordset cnn.Execute(Sql)
Execute语句先执行SQL语句
<span style="color: black;">运用</span>单元格对象的CopyFromRecordset<span style="color: black;">办法</span>将SQL<span style="color: black;">查找</span>到的内容复制到D2单元格为左上角的单元格区域
以上是第三步,执行SQL语句并将数据读入表格指定区域
cnn.Close
关闭链接
Set cnn = Nothing
释放内存
End Sub<h1 style="color: black; text-align: left; margin-bottom: 10px;">代码解析</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码我放了一个例子,大部分的代码是一致的,<span style="color: black;">仅有</span>SQL的句子不<span style="color: black;">同样</span>。<span style="color: black;">必须</span><span style="color: black;">重视</span>的是:仓位=2号仓 2好仓的的符号是英文输入法下的单引号,不是双引号。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">点击关注<span style="color: black;">能够</span>更方便的查看Excel VBA的案例<span style="color: black;">文案</span></h1>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">私信 SQL <span style="color: black;">能够</span>获取SQL代码的Excel文件</h1>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">私信 视频 <span style="color: black;">能够</span>获取54集VBA入门视频</h1>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">私信 VBA或 vba <span style="color: black;">能够</span>获取<span style="color: black;">文案</span>中含VBA代码的Excel文件</h1>
</div>
大势所趋,用于讽刺一些制作目的就是为了跟风玩梗,博取眼球的作品。 我赞同你的看法,你的智慧让人佩服,谢谢分享。
页:
[1]