一个查询数据的问题?请各位大虾帮忙!

babirui 2003-01-03 12:45:01
有下面一些文本框:
aa:<input type=text name=aa value=aa>
bb:<input type=text name=bb value=bb>
cc:<input type=text name=cc value=cc>
dd:<input type=text name=dd value=dd>
ee:<input type=text name=ee value=ee>

我想这样查询:如果aa,bb里面有数据,那么查询的条件是根据aa,bb查询,如下:

String sql="select * from ajgl where aa like '%"+aa+"%' and bb like '%"+bb"%'";

如果bb,cc,ee里面有数据,那么查询的条件是根据bb,cc,ee查询,如下

String sql="select * from ajgl where bb like '%"+bb+"%' and cc like '%"+cc"%' and ee like '%"+ee+"%'";

如果所有的文本框里都没有数据的话,查询的条件是:

String sql="select * from ajgl";
...全文
45 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
nielisheng 2003-01-03
  • 打赏
  • 举报
回复
function condition_build()
dim strwhere
dim strval,strcom
strwhere = ""
if Request("zd_1") <> "" then
strcom = Trim(request("de_1"))
if strcom = "like" then
strval = "'%"&Request("val_1")&"%'"
else
strval = "'"&Request("val_1")&"'"
end if
strwhere = Request("left_c_1")&Request("zd_1")&Request("de_1")&strval&Request("right_1")
end if
'Response.Write strWhere
if Request("zd_2") <> "" then
strcom = Trim(request("de_2"))
if strcom = "like" then
strval = "'%"&Request("val_2")&"%'"
else
strval = "'"&Request("val_2")&"'"
end if
strwhere = strwhere&Request("left_l_2")&Request("left_c_2")&Request("zd_2")&Request("de_2")&strval&Request("right_2")
end if
'Response.Write strWhere
if Request("zd_3") <> "" then
strcom = Trim(request("de_3"))
if strcom = "like" then
strval = "'%"&Request("val_3")&"%'"
else
strval = "'"&Request("val_3")&"'"
end if
strwhere = strwhere&Request("left_l_3")&Request("left_c_3")&Request("zd_3")&Request("de_3")&strval&Request("right_3")
end if
'Response.Write strWhere
if Request("zd_4") <> "" then
strcom = Trim(request("de_4"))
if strcom = "like" then
strval = "'%"&Request("val_4")&"%'"
else
strval = "'"&Request("val_4")&"'"
end if
strwhere = strwhere&Request("left_l_4")&Request("left_c_4")&Request("zd_4")&Request("de_4")&strval&Request("right_4")
end if
'Response.Write strWhere
if Request("zd_5") <> "" then
strcom = Trim(request("de_5"))
if strcom = "like" then
strval = "'%"&Request("val_5")&"%'"
else
strval = "'"&Request("val_5")&"'"
end if
strwhere = strwhere&Request("left_l_5")&Request("left_c_5")&Request("zd_5")&Request("de_5")&strval&Request("right_5")
end if
'Response.Write "where:"
'Response.Write strWhere
'Response.Write ":end where"
'Response.Write "<BR></BR>"
strfilter = strwhere
Set condition_build=strwhere
end function

CrazyFor 2003-01-03
  • 打赏
  • 举报
回复
用IF判断一下就行了,
心翦 2003-01-03
  • 打赏
  • 举报
回复
楼上说的真对.
KnowLittle 2003-01-03
  • 打赏
  • 举报
回复
为了让and不会出错啊,呵呵,不然就要判断前面是否有然后加不加and,所以说这个最简单啊,反正1=1永远成立
babirui 2003-01-03
  • 打赏
  • 举报
回复
请问sql1="1=1"是什么意思?
KnowLittle 2003-01-03
  • 打赏
  • 举报
回复
最简单的办法
sql1="1=1"
if(aa!=""){
sql1=sql+" and aa like '%"+aa+"%'";
}
if(bb!=""){
sql1=sql1+" and bb like '%"+bb+"%'";
}
sql="select * from ajgl where "+sql1
babirui 2003-01-03
  • 打赏
  • 举报
回复
我是这样判断的:
if(aa!=""){
aa="aa like '%"+aa+"%'";
}
if(bb!=""){
aa=aa+"bb like '%"+bb+"%'";
}
sql="select * from ajgl where "+aa;
这样是可以的,但我不知道and 放在哪里,因为aa,bb和组合起来查询,如果是这样的:
aa="aa like '%"+aa+"%' and ";
bb里面如果没有数据的话就出错!
KnowLittle 2003-01-03
  • 打赏
  • 举报
回复
先判断一下,除非没办法,一般不要把(业务逻辑方面)判断扔给SQLSERVER, 加重负担,也增加了依赖
pengdali 2003-01-03
  • 打赏
  • 举报
回复
在asp中判断一下嘛!
var stringSQL
if (aa!="")
stringSQL=..

if (stringSQL!="")
stringSQL="where "+stringSQL
....

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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