子查询
在一个select、select…into、insert…into、delete、或update 表达式中,可以包括select表达式,这个select表达式叫做子查询(sub query)。
您可以使用三种语法建立子查询:
表达式 [any | all | some] (子查询)
表达式 [not] in (子查询)
[not] exists (子查询)
子查询的一个select表达式,与一般select表达式的语法相同,必须包括在括号之中。
您可以使用子查询来替代select表达式的运算式,或在where或 having子句中的运算式。
关键字any和some的意义相同,用来选择符合子查询的任何记录的比较条件。譬如下例将返回产品中单价大于订单中任何数量大于100的记录:
select * from 产品
where 单价 > any
(select 单价 from 订单
where 数量 > 100)
关键字all,用来选择符合子查询的所有记录的比较条件。
譬如在上例中将any改为all,将返回产品中单价大于订单中所有数量大于100的记录。
关键字in 述语来撷取在主查询中且只有在子查询之中包含相同值的某些记录。下列范例会返回以百分之 25 或更高的折扣卖出的所有产品:
关键字in,用来选择在子查询之中的记录。譬如下例将返回订单中数量 > 100的记录:
select * from 产品
where 产品代号 in
(select 产品代号 from 订单
where 数量 > 100)
相反地,关键字not in,用来选择不在子查询之中的记录。
在true/false比较中,可以使用exists关键字,来决定子查询是否会返回任何的记录。
关键字all的asp例子,譬如asp程式rs24.asp如下,[select 姓名,科目,分数 from 考试 where 科目 = 算术 and 分数 >= all (select 分数 from 考试 where 科目=算术 and 姓名=张三)] 找出分数大于或等于张三的算术考试的算术记录:
<%
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 分数 >= all (select 分数 from 考试 where 科目=算术 and 姓名=张三)"
rs2.open sqlstr,conn1,1,1
response.write "<p>all高于张三算术所有分数"
do while not rs2.eof
response.write "<br>" & rs2("姓名") & " " & rs2("科目") & " 分数: " & rs2("分数")
rs2.movenext
loop
rs2.close
%>
以上的 asp程式rs24.asp,在用户端使用浏览器,浏览执行的结果,显示分数大于或等于张三的算术考试的算术记录。
any
关键字any用来选择符合子查询的任何记录的比较条件,譬如asp程式rs24.asp如下,[select 姓名,科目,分数 from 考试 where 科目 = 算术 and 分数 >= any (select 分数 from 考试 where 科目=算术 and 姓名=张三)] 找出分数大于或等于张三任何算术分数的记录:
<%
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 分数 >= any (select 分数 from 考试 where 科目=算术 and 姓名=张三)"
rs2.open sqlstr,conn1,1,1
response.write "<p>any高于张三算术任何分数"
do while not rs2.eof
response.write "<br>" & rs2("姓名") & " " & rs2("科目") & " 分数: " & rs2("分数")
rs2.movenext
loop
rs2.close %>
以上的 asp程式rs24.asp,在用户端使用浏览器,浏览执行的结果,显示分数大于或等于张三任何算术分数的记录。
some
关键字some和any的意义相同,用来选择符合子查询的任何记录的比较条件,譬如asp程式rs24.asp如下,[select 姓名,科目,分数 from 考试 where 科目 = 算术 and 分数 >= some (select 分数 from 考试 where 科目=算术 and 姓名=张三)] 找出分数大于或等于张三任何算术分数的记录:
<%
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 分数 >= some (select 分数 from 考试 where 科目=算术 and 姓名=张三)"
rs2.open sqlstr,conn1,1,1
response.write "<p>some高于张三算术任何分数"
do while not rs2.eof
response.write "<br>" & rs2("姓名") & " " & rs2("科目") & " 分数: " & rs2("分数")
rs2.movenext
loop
rs2.close
%>
以上的 asp程式rs24.asp,在用户端使用浏览器,浏览执行的结果,显示分数大于或等于张三任何算术分数的记录。
select…into
select…into将查询的结果,建立一个产生的表。
语法如下:
select 字段1[,字段2[, …]] into 新表 [in 外部表]
from 表
新表的名称不可与现存表的名称相同,否则将会发生错误。
select…into所建立的新表,其字段的资料类型及大小与所查询的表相同。
让我们看一个于asp程式当中使用这个sql指令的例子。
譬如asp程式rs9.asp如下,[select * into 电脑 from 产品 where 种类 = 电脑] 将 [产品] 表中所有 [种类] 为 [电脑] 的纪录产生一个新的 [电脑] 表:
<%
set conn1 = server.createobject("adodb.connection")
conn1.open "dbq="& server.mappath("ntopsamp.mdb") &";driver={microsoft access driver (*.mdb)};driverid=25;fil=ms access;"
sql = "select * into 电脑 from 产品 where 种类 = 电脑"
set a = conn1.execute(sql)
set rs3 = server.createobject("adodb.recordset")
sql = "select * from 电脑"
rs3.open sql,conn1,1,1,1
%>
<table colspan=8 cellpadding=5 border=0>
<tr>
<td align=center bgcolor="#800000"><font color="#ffffff">代号</font></td>
<td align=center bgcolor="#800000"><font color="#ffffff">名称</font></td>
<td align=center bgcolor="#800000"><font color="#ffffff">价格</font></td>
<td align=center bgcolor="#800000"><font color="#ffffff">数量</font></td>
</tr>
<% do while not rs3.eof %>
<tr>
<td bgcolor="f7efde" align=center><%= rs3("代号")%></td>
<td bgcolor="f7efde" align=center><%= rs3("名称")%></td>
<td bgcolor="f7efde" align=center><%= rs3("价格")%></td>
<td bgcolor="f7efde" align=center><%= rs3("数量")%></td>
</tr>
<%
rs3.movenext
loop
rs3.close
%>
</table>
以上的 asp程式rs9.asp,在用户端使用浏览器,浏览执行的结果,显示新 [电脑] 表的记录。
(全文完)出处:http://asp123.on.net.cn
