帮忙看看这个查询语句的where条件应该怎么写?

simonqsm 2004-07-16 02:34:59
<select name="type" style="width:70px" class="Sborder">
<option value="-1" selected>请选择</option>
<option value="00">选择题</option>
<option value="01">填空题</option>
</select>
<select name="chapter" class="Sborder">
<option value="-1" selected>请选择</option>
<option value="01">第一章——概述</option>
<option value="02">第二章——算法</option>
<option value="03">第三章——数据类型、运算符与表达式</option>
<option value="04">第四章——顺序程序设计</option>
<option value="05">第五章——选择结构程序设计</option>
<option value="06">第六章——循环控制</option>
<option value="07">第七章——数组</option>
<option value="08">第八章——函数</option>
<option value="09">第九章——预处理命令</option>
<option value="10">第十章——指针</option>
<option value="11">第十一章——结构体与共用体</option>
<option value="12">第十二章——位运算</option>
<option value="13">第十三章——文件</option>
<option value="14">综合题</option>
</select>
<select name="diff" class="Sborder" style="width:70px">
<option value="-1" selected>请选择</option>
<option value="00">简单</option>
<option value="01">中等</option>
<option value="02">较难</option>
</select>
<input type="text" name="id" style="width:100px" class="Sborder" maxlength="8">

这三个下拉列表和一个文本框,只要有一个有内容就可以查询,有两个以上要组合查询。这样的查询方法,where条件有什么好的方法去写?

谢谢大家,我写的方法太复杂,分若干种情况。
...全文
73 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
huagou 2004-07-16
  • 打赏
  • 举报
回复
<%
'================取得表单提交过来的数据=========================
type = trim(request.Form("type"))
chapter = trim(request.Form("chapter"))
diff = trim(request.Form("diff"))


'================将查询条件转换为SQL查询指令=====================
if wherestr="" then wherestr=" where "

'如果存在查询条件,则将查询条件加入查询字串
if type <> "-1" then wherestr = wherestr & "and (type=('" & type & "')) "
if chapter <> "-1" then wherestr = wherestr & "and (chapter>=('" & chapter & "')) "
if diff <> "-1" then wherestr = wherestr & "and (diff<=('" & diff & "')) "

'================去除第一个查询条件和 where 之间的 and============

wherestr=replace(wherestr,"where and","where")

'================如果没有查询条件,则查询字串应为空===============

if trim(wherestr)="where" then wherestr=""

sql="select * from table " &wherestr

%>
stefli 2004-07-16
  • 打赏
  • 举报
回复
sql = "select * from table where"

if a<>"" then
sql = sql & " fileda like '%" & a & "%' and"
end if

if b<>"" then
sql = sql & " filedb like '%" & b & "%' and"
end if

if c<>"" then
sql = sql & " filedc like '%" & c & "%' and"
end if

sql = sql & "1=1"

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧