注册 登录
编程论坛 ASP技术论坛

ASP+access搜索条输入一个或多个关键词查询的问题

cwang2100 发布于 2010-03-24 16:01, 684 次点击
同一文本框输入一个或多个用空格分格的关键词,合在一起SQL代码怎么写,本人是新手,想学习ASP编程,希望能得到大家的帮忙,谢谢!!

cxsj=request("cxsj")
cxsj=Rtrim(LTrim(cxsj))
bb=request("bb")
dd=request("dd")
SearchUC=request("SearchUC")
dim pageCount
page = cint(request("page"))

set conn=server.createobject("adodb.connection")
set rs=server.createobject("adodb.recordset")
conn.open "DBQ=" & server.mappath("test.mdb") & ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"

sql="select * from tale where 1=1 "

sql="select * from tale where feng like '%%' "

Dim SearchStr
-----------------------------------------
if cxsj<>"" then
sql=sql&"and minqing like '%"&cxsj&"%'"
end if

’这代码只能单个关键词查询,加空格后再加一个字就不能查询了,如:中国 人 这就查不出来,中国,或人分开就可查到。
--------------------------------------------
if instr(cxsj,"")<>0 then ’
str = split(cxsj,"")
for i=0 to ubound(str)
sql = sql&" and minqing like'%"&str(i)&"%'"
next
end if

’这多个空格分开的关键词能查询到,但单个词就查不到了,如:中国 就查不到,中国 人 就能查到
----------------------------------------------------------------
if bb<>"" then
sql=sql&"and dindi like '%"&bb&"%'"
end if

if dd<>"" then

sql=sql&"and xodntad like '%"&dd&"%'"

end if

if SearchUC="" then

sql=sql&" order by id desc"

end if

if SearchUC="1" then
sql=sql&" and ping between 0.01 and 100.00 order by ping asc"

end if

怎样才能输入单个或输入多个空格分格的关键词都能用呢?怎样的判断语句才行,谢谢!!
1 回复
#2
pmaojie2010-03-25 17:03
应该用OR吧!
如:
程序代码:
<%
KeyWord
= "中国 人"'//如关键词是中国 人
KeyWord = Split(KeyWord," ")'//按空格分组


Dim searchStr
searchStr
= ""

'//组合关键词
For I=0 to Ubound(KeyWord)
  
If searchStr="" Then searchStr = " And minqing Like '"&KeyWord(I)&"'" _
  
Else searchStr = searchStr&" Or minqing Like '"&KeyWord(I)&"'"
Next

Sql
="Select * From [tale] where 1=1 "
Sql
=  Sql& searchStr

Response.write Sql
%>

1