|
"比如说你现在在做个查询表单<BR><form method=post action=test.asp><BR><p>name:<input name=tName></P><BR><p>contents keyword:<input name=tCkeyword></P><BR><p>topic keyWord:<input name=tTkeyword></P><BR><p><input type=submit value=""test""></P><BR></form><BR>里面有三个查询条件,用户可以使用一个,二个或三个条件(当然,要多个的话自己加),以应表中uName,Topic,Contents三个字段<BR>在查询页面test.asp里你是不是要写复杂的IF语句呢?<BR>用到 where 1=1,就可以方便解决你的代码.<BR>test.asp<BR>---------------------------------<BR>dim nameString,Tstring,Cstring<BR>nameString=""<BR>Tstring=""<BR>Cstring=""<BR>if request("tname")<>"" then nameString=" and uName='" & checkString(request("tName")) & "'"<BR>if request("tTkeyword")<>"" then TString=" and Topic like '%" & checkString(request("tTkeyword")) & "%'"<BR>if request("tCkeyword")<>"" then CString=" and content like '%" & checkString(request("tCkeyword")) & "%'"<BR>sql="select * from table where 1=1" & nameString & Tstring & Cstring<BR>......<BR><BR>注:上面的checkString是非法字符处理函数,在此篇中不是重点,不做介绍<BR>如果不使用 where 1=1 那么当条件顺序变了,或当某个条件不存在时,存在着对某个条件string中对"where " 或 " and "之类命令字比较复杂的处理.<BR><BR>1=1 返回 1<BR>从逻辑运算上来讲<BR>1 and x=x 这是一条法则<BR>那么<BR>1 and (a and b) 逻辑结果等于 a and b<BR>1 and a or b ==> (1 and a) or b ==> a or b<BR>即,多加 1=1 不会对逻辑判断造成任何影响. 反而,在多件的SQL组合语句字串的构造上获取了方便.<BR> |