最近在做公司網站,為了讓客戶更容易地尋找公司的產品以及新聞等資訊,要實現站內簡單搜尋.如果簡單地採用SQL的LIKE '%Keyword%'方法效率實在不高.比如公司主打產品IPBOARD,如果你輸入"IP BOARD"就找不到了.想了個辦法解決.構造SQL語句核心代碼如下:
Dim act,Key,i,sql
act=trim(request.QueryString("action"))
if act="search" then
Key=trim(request.Form("Keyword"))
if requestCstringSafe(Key)="" then Dimerr "搜尋字串中含有非法字元,或者為空白","back"
Key=replace(Key," "," ")
Key=split(Key," ")
'查詢JL_Subject表,構造SQL語句
sql="select * from JL_Subject where "
sql=sql&"(Keyword Like '%"&Key(0)&"%' " '搜尋Keyword欄位
for i=Lbound(Key)+1 to Ubound(Key)
if Key(i)<>"" and UCase(Key(i))<>"OR" and UCase(Key(i))<>"AND" then if UCase(Key(i-1))="OR" then
sql=sql&" or Keyword Like '%"&Key(i)&"%'"
else
sql=sql&" and Keyword Like '%"&Key(i)&"%'"
end if
end if
next
sql=sql&") or (Title Like '%"&Key(0)&"%' "
for i=Lbound(Key)+1 to Ubound(Key)
if Key(i)<>"" and UCase(Key(i))<>"OR" and UCase(Key(i))<>"AND" then
if UCase(Key(i-1))="OR" then
sql=sql&" or Title Like '%"&Key(i)&"%'"
else
sql=sql&" and Title Like '%"&Key(i)&"%'"
end if
end if
next
sql=sql&")"
response.Write(sql)
end if OK,看看這樣的SQL語句句應該沒問題了吧...呵呵... 其實做站內搜尋還有個投機取巧的辦法:利用google或者百度的site:domainroot文法進行指定站搜尋,將站內的搜尋提交到google或者百度的伺服器上(這樣做的前提是你的頁面必須事先被百度跟google收錄了,要不然是搜取不到D...嘿嘿...)當然,如果你懂小偷技術原理的話,你還可以將提交返回的頁面修改成你的風格,這樣不仔細看的話還真不知道你是提交到google或者百度返回的結果.呵呵...