SQL中select语句用法集锦

SQL语句
551
0
0
2022-08-16
标签   SQL语句

SQL中select语句用法集锦

一、条件筛选

1、数字筛选:sql = "Select * from [sheet1$] Where 销售单价 > 100"

2、字符条件:sql = "Select * from [sheet1$] Where 物品名称 ='挡泥板'"

3、日期条件:sql = "Select * from [sheet1$] Where 物品名称 ='挡泥板'"

4、区间条件:sql = "Select * from [sheet1$] Where 出库日期 between #2005-1-4# and #2005-1-10#"

5、多个条件:用and,OR连接

'sql = "Select * from [sheet1$] Where 出库日期 between #2005-1-4# and #2005-1-10# and 销售单价>100"

6、模糊条件:%表示任意多个字符,_(下划线)表示单个占位符

'sql = "Select * from [sheet1$] Where 物品名称 like '%扶手%'"

7、插入变量查询:

'sr = "挡泥板"

'sql = "Select * from [sheet1$] Where 物品名称 ='" & sr & "'"

8、在字符串组里:

'sql = "Select * from [sheet1$] Where 物品名称 in('车衣','扶手箱')"

9、借用函数优化:

'sql = "Select * from [sheet1$] Where left(物品代码,3)='028'"

二、指定筛选

1、筛选全部字段:*表示全部字段

'sql = "Select * from ChuKu"

2、筛选指定字段:在from前面列出要显示的所有字段,如果要跳过的用""""

'sql = "Select """",出库日期,"""",物品代码,"""",规格,单位 from ChuKu"

3、筛选不重复的:Distinct 字段名 筛选不重复的记录

'sql = "Select Distinct """",物品代码 from ChuKu"

4、按百分比筛选:Top N percent 可以显示前百分之N的记录

'sql = "Select top 30 Percent * from ChuKu"

'对筛选的字段用函数进行进一步的处理和格式化

'sql = "Select ID,Format(出库日期,""yyyy-mm-dd"") from ChuKu"

5、排序:Desc降序,Asc升序

'sql = "Select * from ChuKu Order by 出库日期 asc,销售单价 desc"

'sql = "Select Top 10 * from ChuKu Order by 出库日期 asc,销售单价 desc" '筛选销售数量前10()

三、分组显示

Group by 可以配合函数进行分组求和,分组求最大值等。

'sql = "Select """","""",物品代码,"""","""","""",sum(出库数量) from ChuKu group by 物品代码" ’ 跳过的用""""

按条件显示分组记录

'sql = "Select """","""",物品代码,"""","""","""",sum(出库数量) from ChuKu group by 物品代码 HAVING sum(出库数量)>=3"