欢迎光临
我们一直在努力

在ADO使用SELECT语法四

建站超值云服务器,限时71元/月

having

having使用于select 表达式中,筛选已经group by统计的记录。在group by统计记录后,having将筛选与having子句中条件相吻合的记录。

语法如下:

select fieldlist

from table

where selectcriteria

group by groupfieldlist

[having groupcriteria]

.groupcriteria表示决定应筛选的统计记录。

having与where相类似,是用来决定选取哪些记录。当使用group by来统计记录后,having会决定应显示的记录,譬如:

select 产品名称

from 产品

group by 分类

having 单价 > 1000

一个having子句最多可包含40个运算式,运算式之间将由and或or等逻辑运算子来连结。

让我们看一个于asp程式当中使用这个sql指令的例子。

我们可以利用having子句决定应显示的记录,譬如asp程式rs23.asp如下,[select 姓名,科目,avg(分数) as 平均 from 考试 group by 姓名,科目 having avg(分数) >=60],使用having avg(分数) >=60找出平均分数大于或等于60分的记录:

<%

set conn1 = server.createobject("adodb.connection")

conn1.open "dbq=" & server.mappath("ntopsamp.mdb") & ";driver={microsoft access driver (*.mdb)};driverid=25;fil=ms access;"

set rs2 = server.createobject("adodb.recordset")

sqlstr = "select 姓名,科目,avg(分数) as 平均 from 考试 group by 姓名,科目 having avg(分数) >=60"

rs2.open sqlstr,conn1,1,1

response.write "<p>having avg(分数) >=60"

do while not rs2.eof

response.write "<br>" & rs2("姓名") & " " & rs2("科目") & " 平均: " & rs2("平均")

rs2.movenext

loop

rs2.close

%>

以上的 asp程式rs23.asp,在用户端使用浏览器,浏览执行的结果,显示找出平均分数大于或等于60分的记录。

我们也可以利用having子句找出重复的记录,譬如asp程式rs23.asp如下,[select 代号 from 产品 group by 代号 having count(代号) > 1],使用having count(代号) > 1找出代号重复的记录:

<%

set conn1 = server.createobject("adodb.connection")

conn1.open "dbq=" & server.mappath("ntopsamp.mdb") & ";driver={microsoft access driver (*.mdb)};driverid=25;fil=ms access;"

set rs2 = server.createobject("adodb.recordset")

sqlstr = "select 代号 from 产品 group by 代号 having count(代号) > 1"

rs2.open sqlstr,conn1,1,1

response.write "<p>找出重复having count(代号) > 1"

do while not rs2.eof

response.write "<br>" & rs2("代号")

rs2.movenext

loop

rs2.close

%>

以上的 asp程式rs23.asp,在用户端使用浏览器,浏览执行的结果,显示代号重复的记录。

union

union可以合并多组查询的结果。

语法如下:

查询1 union [all] 查询2 [union [all]查询3 [ … ]]

查询为一个select表达式。

当您使用一个 union 运算时,不会返回重复的记录;若要返回所有的记录,您可以于union后加上all,加上all执行查询的速度比较快。

在一个union运算中的所有查询,字段数目必须相同。字段大小可以不同,字段资料类型也可以不同。

只有在第一个select表达式中可使用别名,在其它select表达式中被省略。

可以在每一个select表达式中使用group by或having子句,以统计查询的结果。

可以在最后一个select表达式使用order by 子句,以指定查询的结果的排列顺序。

让我们看一个于asp程式当中使用这个sql指令的例子。

可以利用union合并两组查询的结果,譬如asp程式rs25.asp如下,[(select 姓名,科目,分数 from 考试 where 科目=算术 and 姓名=李四) union (select 姓名,科目,分数 from 考试 where 科目=算术 and 姓名=张三)],使用union合并两组select查询的结果,一组为查询李四的算术成绩记录,另一组查询张三的算术成绩记录:

<%

set conn1 = server.createobject("adodb.connection")

conn1.open "dbq=" & server.mappath("ntopsamp.mdb") & ";driver={microsoft access driver (*.mdb)};driverid=25;fil=ms access;"

set rs2 = server.createobject("adodb.recordset")

sqlstr = "(select 姓名,科目,分数 from 考试 where 科目=算术 and 姓名=李四) union (select 姓名,科目,分数 from 考试 where 科目=算术 and 姓名=张三)"

rs2.open sqlstr,conn1,1,1

response.write "<p>union"

do while not rs2.eof

response.write "<br>" & rs2("姓名") & " " & rs2("科目") & " 分数: " & rs2("分数")

rs2.movenext

loop

rs2.close

%>

以上的 asp程式rs25.asp,在用户端使用浏览器,浏览执行的结果,显示李四和张三的算术分数记录。

在ado使用select语法系列转载自http://asp123.on.net.cn

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在ADO使用SELECT语法四
分享到: 更多 (0)