ASP做多个条件搜索的主要代码是怎样的?

2025-05-23 09:29:26
推荐回答(4个)
回答1:

加代码控制,用if判断后再组合sql语句,1=1 是为了接and条件,可以修改成一个必填项

'建立连接
conn=……

'指定查询条件
sql="select * from 表 where 1=1"

if 地址<>"" then
sql = sql & "and 地址='上海'"
end if

if 性别<>"" Then
sql = sql & "and 性别="男""
end if

if 性别<>"" then
sql = sql & "and 职业='工程师'"
end if

……

'执行查询
rs.open sql,conn,1,3

'循环列出查询到的纪录集字段
do while not rs.eof
……
loop

回答2:

搜索条件
sql="select * from 表 where 地址='上海' or 性别="男" or 职业='工程师'"
比较好,用or

回答3:

AddText request("AddText ")
SexText request("SexText ")
StrSQL ="select * from 表 where
StrSQL = StrSQL & " address like '%" & AddText &"%' "
StrSQL = StrSQL & "and sex like '%" & SexText &"%' "
...

回答4:

sql="select * from 表 where 职业='工程师'"
if 性别<>"" then
sql=sql&" and 性别='"&request("性别")&"'"
end if
if 地址<>"" then
sql=sql&" and 地址='"&request("地址")&"'"
end if