經(jīng)過(guò)多次研究寫(xiě)出了如下代碼,有需要的可以參考下
kd=server.HTMLEncode(request("keyword"))
if kd<>"" then
kd=trim(kd)
'kd=replace(kd," ","")
kd=replace(kd,"'","")
kd=replace(kd,"%","")
kd=replace(kd,"\"," ")
kd=replace(kd,">",">")
kd=replace(kd,"<","<")
kd=replace(kd,","," ")
kd=replace(kd,","," ")
kd=replace(kd,"|"," ")
kd=replace(kd,";"," ")
kd=replace(kd,":"," ")
kd=replace(kd,":"," ")
kd=replace(kd,";"," ")
'上面的是先替換一些特殊字符,方便輸入一些特殊的分隔符
keyarr= Split(kd," ")
keyarrl=ubound(keyarr)
For I = 0 to keyarrl
if keyarrl>0 then
sqlk=sqlk&" and title like '%"&keyarr(I)&"%'"
else
sqlk=sqlk&"and title like '%"&keyarr(I)&"%'"
end if
Next
if id<>"" then
sql="select top 1000 id,title from news where type_id in ("&sqqq&") "&sqlk&" order by isshow ,shengcheng,id desc"
else
sql="select top 1000 id,title from news where id<>0 "&sqlk&" order by isshow ,shengcheng,id desc"
end if
else
if id<>"" then
sql="select top 1000 id,title from news where type_id in ("&sqqq&") order by isshow ,shengcheng,id desc"
else
sql="select top 1000 id,title from news where id<>0 order by isshow ,shengcheng,id desc"
end if
end if
ASP 多條件符合查詢(xún)代碼實(shí)例2
asp的多條件符合查詢(xún)語(yǔ)句,自己寫(xiě)的,拿出來(lái)分享一下,絕對(duì)原創(chuàng)。請(qǐng)多指教!
<%
dim qy
qy=0
if request.form("stu_name")<>"" then '第一個(gè)條件表單傳遞的數(shù)據(jù)
str="stu_name='"&request.form("stu_name")&"'"
qy=qy+1
end if
if request.form("stu_num")<>"" then '第二個(gè)條件表單傳遞的數(shù)據(jù)
if qy=0 then
str=str&"stu_number='"&request.form("stu_num")&"'"
else
str=str&"and stu_number='"&request.form("stu_num")&"'"
end if
qy=qy+1
end if
if request.form("stu_xibie")<>"" then '第三個(gè)條件表單傳遞的數(shù)據(jù)
if qy=0 then
str=str&"stu_xibie='"&request.form("stu_xibie")&"'"
else
str=str&"and stu_xibie='"&request.form("stu_xibie")&"'"
end if
qy=qy+1
end if
if request.form("stu_class")<>"" then
if qy=0 then
str=str&"stu_class='"&request.form("stu_class")&"'"
else
str=str&"and stu_class='"&request.form("stu_class")&"'"
end if
qy=qy+1
end if
if request.form("stu_year")<>"" then
if qy=0 then
str=str&"stu_year='"&request.form("stu_year")&"'"
else
str=str&"and stu_year='"&request.form("stu_year")&"'"
end if
qy=qy+1
end if
sql="select * from [students] where "&str
Set rs=Conn.Execute(sql) '執(zhí)行sql語(yǔ)句
%>
運(yùn)行環(huán)境:IIS
腳本語(yǔ)言:VBScript
數(shù)據(jù)庫(kù):Access/SQL Server
數(shù)據(jù)庫(kù)語(yǔ)言:SQL
1.概要:
不論是在論壇,還是新聞系統(tǒng),或是下載系統(tǒng)等動(dòng)態(tài)網(wǎng)站中,大家經(jīng)常會(huì)看到搜索功能:搜索帖子,搜索用戶(hù),搜索軟件(總之搜索關(guān)鍵字)等,本文則是介紹如何建立一個(gè)高效實(shí)用的,基于ASP的站內(nèi)多值搜索。
本文面對(duì)的是“多條件模糊匹配搜索”,理解了多條件的,單一條件搜索也不過(guò)小菜一碟了。一般來(lái)講,有兩種方法進(jìn)行多條件搜索:枚舉法和遞進(jìn)法。搜索條件不太多時(shí)(n<=3),可使用枚舉法,其語(yǔ)句頻度為2的n次方,成指數(shù)增長(zhǎng),n為條件數(shù)。很明顯,當(dāng)條件增多以后,無(wú)論從程序的效率還是可實(shí)現(xiàn)性考慮都應(yīng)采用遞進(jìn)法,其語(yǔ)句頻度為n,成線性增長(zhǎng)。需要指出的是,枚舉法思路非常簡(jiǎn)單,一一判斷條件是否為空,再按非空條件搜索,同時(shí)可以利用真值表技術(shù)來(lái)對(duì)付條件極多的情況(相信沒(méi)人去干這種事,4條件時(shí)就已經(jīng)要寫(xiě)16組語(yǔ)句了);遞進(jìn)法的思想方法較為巧妙,重在理解,其巧就巧在一是使用了標(biāo)志位(flag),二是妙用SQL中字符串連接符&。下面以實(shí)例來(lái)講解引擎的建立。
2.實(shí)例:
我們建立一通訊錄查詢(xún)引擎,數(shù)據(jù)庫(kù)名為addressbook.mdb,表名為address,字段如下:
ID Name Tel School
1 張 三 33333333 電子科技大學(xué)計(jì)算機(jī)系
2 李 四 44444444 四川大學(xué)生物系
3 王 二 22222222 西南交通大學(xué)建筑系
… … … …
Web搜索界面如下:
姓名: 電話: 學(xué)校: 搜索按鈕
采用枚舉法的源程序如下:
<%@ CODEPAGE = "936" %>
'連接數(shù)據(jù)庫(kù)
<%
dim conn
dim DBOath
dim rs
dim sql
Set conn=Server.CreateObject("ADODB.Connection")
DBPath = Server.MapPath("addressbook.mdb")
conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath
Set rs=Server.CreateObject("ADODB.Recordset")
'從Web頁(yè)獲取姓名、電話、學(xué)校的值
dim Name
dim Tel
dim School
Name=request("Name")
Tel=request("Tel")
School=request("School")
'枚舉法的搜索核心,因?yàn)橛?個(gè)條件所以要寫(xiě)8組If判斷語(yǔ)句
if trim(Name)="" and trim(Tel)="" and trim(School)="" then
sql="select * from address order by ID asc"
end if
if trim(Name)="" and trim(Tel)="" and trim(School)<>"" then
sql="select * from address where School like '%"&trim(School)&"%' order by ID asc"
end if
if trim(Name)="" and trim(Tel)<>"" and trim(School)="" then
sql="select * from address where Tel like '%"&trim(Tel)&"%' order by ID asc"
end if
if trim(Name)="" and trim(Tel)<>"" and trim(School)<>"" then
sql="select * from address where Tel like '%"&trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc"
end if
if trim(Name)<>"" and trim(Tel)="" and trim(School)="" then
sql="select * from address where Name like '%"&trim(Name)&"%' order by ID asc"
end if
if trim(Name)<>"" and trim(Tel)="" and trim(School)<>"" then
sql="select * from address where Name like '%"&trim(Name)&"%' and School like '%"&trim(School)&"%' order by ID asc"
end if
if trim(Name)<>"" and trim(Tel)<>"" and trim(School)="" then
sql="select * from address where Name like '%"&trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' order by ID asc"
end if
if trim(Name)<>"" and trim(Tel)<>"" and trim(School)<>"" then
sql="select * from address where Name like '%"&trim(Name)&"%' and Tel like '%"&trim(Tel)&"%' and School like '%"&trim(School)&"%' order by ID asc"
end if
rs.open sql,conn,1,1
'顯示搜索結(jié)果
if rs.eof and rs.bof then
response.write "目前通訊錄中沒(méi)有記錄"
else
do while not rs.eof
response.write "姓名:"&rs("Name")&"電話:"&rs("Tel")&"學(xué)校:"&rs("School")&"<br>"
rs.movenext
loop
end if
'斷開(kāi)數(shù)據(jù)庫(kù)
set rs=nothing
conn.close
set conn=nothing
%>
理解上述程序時(shí),著重琢磨核心部分,8組語(yǔ)句一一對(duì)應(yīng)了3個(gè)搜索框中的8種狀態(tài)
Name Tel School
空 空 空
空 空 非空
空 非空 空
空 非空 非空
非空 空 空
非空 空 非空
非空 非空 空
非空 非空 非空
另外trim()是VB的函數(shù),將輸入的字符串前后的空格去掉;%是SQL語(yǔ)言中的多字符通配符(_是單字符通配符),由此可見(jiàn)%"&trim()&"%對(duì)搜索框中輸入的關(guān)鍵字是分別向左向右匹配的;SQL語(yǔ)言中用and連接說(shuō)明非空條件之間是“與”關(guān)系。
再來(lái)看看遞進(jìn)法,與枚舉法相比它們只有核心部分不同:
'遞進(jìn)法的搜索核心,依次判斷條件為空否,非空則將其加入搜索條件
sql="select * from address where"
if Name<>"" then
sql=sql&" Name like '%"&Name&"%' "
flag=1
end if
if Tel<>"" and flag=1 then
sql=sql&" and Tel like '%"&Tel&"%'"
flag=1
elseif Tel<>"" then
sql=sql&" Tel like '%"&Tel&"%'"
flag=1
end if
if Company<>"" and flag=1 then
sql=sql&" and Company like '%"&Company&"%'"
flag=1
elseif Company <>"" then
sql=sql&" Company like '%"&Company&"%'"
flag=1
end if
if flag=0 then
sql="select * from address order by ID asc"
end if
rs.open sql,conn,1,1
遞進(jìn)法是一個(gè)明智的算法,單從語(yǔ)句的長(zhǎng)短就可以看出來(lái)了。這個(gè)算法的難點(diǎn)和精髓就在flag和&上。首先你應(yīng)該清楚&在SQL中就是一個(gè)字符串連接符,把該符號(hào)左右的字符拼接在一起。再回到程序,當(dāng)Name不為空時(shí)sql="select * from address where Name like '%"&Name&"%' "同時(shí)flag=1;接下來(lái)當(dāng)Name不為空時(shí)且Tel不為空時(shí),即Tel<>"" and flag=1時(shí),sql="select * from address where Name like '%"&Name&"%' and Tel like '%"&Tel&"%' "同時(shí)flag=1,否則當(dāng)Name為空Tel不為空,sql="select * from address where Tel like '%"&Tel&"%' "同時(shí)flag=1;以此類(lèi)推就可以推廣到n個(gè)條件的搜索。當(dāng)然條件皆為空時(shí),即flag=0將選擇所有表中所有項(xiàng)。
3.驗(yàn)證:
至此,一個(gè)搜索引擎就建立起來(lái)了。以下是一些使用示例:
姓名:張 電話: 學(xué)校: 搜索按鈕
搜索結(jié)果為:
姓名: 張三 電話:33333333 單位:電子科技大學(xué)計(jì)算機(jī)系
姓名: 電話: 學(xué)校:大學(xué) 搜索按鈕
搜索結(jié)果為:
姓名:張三 電話:33333333 單位:電子科技大學(xué)計(jì)算機(jī)系
姓名 李 四 電話:44444444 單位:四川大學(xué)生物系
姓名:王二 電話:22222222 單位:西南交通大學(xué)建筑系
姓名: 電話:4444 學(xué)校:四川 搜索按鈕
搜索結(jié)果為:
姓名 李 四 電話:44444444 單位:四川大學(xué)生物系
姓名: 電話: 學(xué)校:交%大 搜索按鈕
搜索結(jié)果為:
姓名:王二 電話:22222222 單位:西南交通大學(xué)建筑系
4.改進(jìn):
其實(shí)這個(gè)引擎還有些缺陷,問(wèn)題主要在于通配符%。一方面是因?yàn)槿藗兤綍r(shí)習(xí)慣把*作為通配符,另一方面%若出現(xiàn)在超鏈接中,通過(guò)request獲取時(shí)%將被“吃”掉,如下:
--test.htm--
…
<a href=test.asp?content=test%the%sign>click here</a>
…
--test.asp--
<%
content=request(“content”)
response.write content
%>
在IE中瀏覽test.htm時(shí)點(diǎn)擊超鏈接,顯示為:
testthesign
可見(jiàn)%直接被超鏈接忽略掉了。怎么才能解決這個(gè)問(wèn)題呢?很簡(jiǎn)單,我們做點(diǎn)小小的手腳--偷梁換柱。
將以下代碼加在搜索核心之前:
Name=replace(Name,"*","%")
Tel=replace(Tel,"*","%")
Company=replace(Company,"*","%")
將以下代碼加在搜索核心之后:
Name=replace(Name,"%","*")
Tel=replace(Tel,"%","*")
Company=replace(Company,"%","*")
在我們來(lái)分析一下這些語(yǔ)句。replace()是VB中字符串替換函數(shù),replace(Name,"*","%") 就是將Name中所有的*換成%。也就是說(shuō),我們把3個(gè)條件中凡是出現(xiàn)的*都替換為%,這樣一來(lái)前3句就將通配符改成*了。而后3句就可以防止%被“吃”掉。所有問(wèn)題就迎刃而解了吧。
姓名: 電話: 學(xué)校:交%大 搜索按鈕
搜索結(jié)果為:
姓名:王 二 電話:22222222 單位:西南交通大學(xué)建筑系
將上面的語(yǔ)句再改一改,把*用空格代替,不就成了我們?cè)贕oogle、BaiDu中常用的用空格來(lái)分開(kāi)搜索條件的搜索引擎了嗎?
補(bǔ)充功能:如果我們要實(shí)現(xiàn)查詢(xún)同一個(gè)表中的標(biāo)題和內(nèi)容 但想按這二個(gè)的順序來(lái)排列 比如 查到與標(biāo)題符合的先顯示出來(lái) 而與內(nèi)容符合的則顯示在標(biāo)題的后面 如何實(shí)現(xiàn)呢?
sql="select * from product where title like '%"&keyword&"%' "
sql=sql + " union select * from product where content like '%"&keyword&"%' order by id desc"
兩條SQL語(yǔ)句中間加一個(gè),union 就可以聯(lián)合查詢(xún),但列必須一樣,還有排序條件也同樣是一個(gè).
我們將用到UNION的聯(lián)合查詢(xún),這將能實(shí)現(xiàn)以上的功能。
分析:數(shù)據(jù)庫(kù)查詢(xún) 將先按與title的數(shù)據(jù)實(shí)現(xiàn)查詢(xún) 然后再將實(shí)現(xiàn)與content的數(shù)據(jù)查詢(xún) 故則有先后之分。