sql = "select * from XXX where 1 = 1"
if a条件成立 then
sql = sql & "and" & "a字段条件语句"
end if
if b条件成立 then
sql = sql & "and" & "b字段条件语句"
end if
if c条件成立 then
sql = sql & "and" & "c字段条件语句"
end if
执行sql
if a条件成立 then
sql = sql & "and" & "a字段条件语句"
end if
if b条件成立 then
sql = sql & "and" & "b字段条件语句"
end if
if c条件成立 then
sql = sql & "and" & "c字段条件语句"
end if
执行sql
var strsqlwhere="";
if(name.value!=""){
if(strsqlwhere!="") strsqlwhere+=" and ";
strsqlwhere+="nvc_name like '%"+name.value+"'%";
}
其它类似
影响效率
where 1=1是什么意思?
把where后面的各个复合查询的条件义字符窜的形式存储在数组中,到最后就根据用户的选择来在数组中选取相应的条件再组合成一个完整的sql语句。大家说这种方法怎么样呢?
sql = "select * from XXX where 1 = 1"
if name<>"" then sql=sql+" name=Request.Form("name") "
if sex<>"" then sql=sql+" name=Request.Form("sex") "
if age<>"" then sql=sql+" name=Request.Form("age") "
rs.Open sql,conn,1,1
影响效率
///////////////////////////////////多谢指正!
这种写法在执行的时候每条记录要比较一次1=1
有一万条记录就比较一万次,再快也需要时间啊