ASP多条件查询

2025-03-22 20:44:14
推荐回答(3个)
回答1:

asp多条件符合查询,参考代码如下:
<%
dim qy
qy=0
if request.form("stu_name")<>"" then '第一个条件表单传递的数据
str="stu_name='"&request.form("stu_name")&"'"
qy=qy+1
end if

if request.form("stu_num")<>"" then '第二个条件表单传递的数据
if qy=0 then
str=str&"stu_number='"&request.form("stu_num")&"'"
else
str=str&"and stu_number='"&request.form("stu_num")&"'"
end if
qy=qy+1
end if

if request.form("stu_xibie")<>"" then '第三个条件表单传递的数据
if qy=0 then
str=str&"stu_xibie='"&request.form("stu_xibie")&"'"
else
str=str&"and stu_xibie='"&request.form("stu_xibie")&"'"
end if
qy=qy+1
end if

if request.form("stu_class")<>"" then
if qy=0 then
str=str&"stu_class='"&request.form("stu_class")&"'"
else
str=str&"and stu_class='"&request.form("stu_class")&"'"
end if
qy=qy+1
end if

if request.form("stu_year")<>"" then
if qy=0 then
str=str&"stu_year='"&request.form("stu_year")&"'"
else
str=str&"and stu_year='"&request.form("stu_year")&"'"
end if
qy=qy+1
end if
sql="select * from [students] where "&str
Set rs=Conn.Execute(sql) '执行sql语句
%>

回答2:

代码我就不写啦,给你思路,自己想出来的会一直记得,别人代码看一遍过几天就会忘记。

首先接收request传递参数,分开接收,在判断接收的后赋值的变量是否为空,

例如:

接收a=request.form(“a”)
b=request.form(“b”)
判断if a=“”and b<>"" then
执行条件查询(这里只需要查询a)
以此类推

回答3:

<%
if request.querystring("act")="cha" Then
caizhi=request("caizhi")
guige=request("guige")
chandi=request("chandi")
huoyuan=request("huoyuan")
if caizhi="" and guige="" and chandi="" and huoyuan="" then
exec="select * from juanban where 1=1"
else
exec="select * from juanban where 材质 like '"&caizhi&"' or 规格 like '"&guige&"' or 产地 like '"&chandi&"' or 货源 like '"&huoyuan&"'"
end if
end if
set rs2=server.createobject("adodb.recordset")
rs2.open exec,conn,1,1
do while not rs2.eof %>