ASP多條件查詢功能實現代碼(多關鍵詞查詢)_應用技巧

來源:互聯網
上載者:User

經過多次研究寫出了如下代碼,有需要的可以參考下

複製代碼 代碼如下:

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 多條件符合查詢代碼執行個體2

asp的多條件符合查詢語句,自己寫的,拿出來分享一下,絕對原創。請多指教!

複製代碼 代碼如下:

<%
 dim qy
  qy=0
  if request.form("stu_name")<>"" then       '第一個條件表單傳遞的資料
   str="stu_name='"&request.form("stu_name")&"'"
   qy=qy+1
  end if

  if request.form("stu_num")<>"" then     '第二個條件表單傳遞的資料
   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     '第三個條件表單傳遞的資料
   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)   '執行sql語句
%>


運行環境:IIS
指令碼語言:VBScript
資料庫:Access/SQL Server
資料庫語言:SQL

1.概要:
不論是在論壇,還是新聞系統,或是下載系統等動態網站中,大家經常會看到搜尋功能:搜尋文章,搜尋使用者,搜尋軟體(總之搜尋索引鍵)等,本文則是介紹如何建立一個高效實用的,基於ASP的站內多值搜尋。

本文面對的是“多條件模糊比對搜尋”,理解了多條件的,單一條件搜尋也不過小菜一碟了。一般來講,有兩種方法進行多條件搜尋:枚舉法和遞進法。搜尋條件不太多時(n<=3),可使用枚舉法,其語句頻度為2的n次方,成指數增長,n為條件數。很明顯,當條件增多以後,無論從程式的效率還是可實現性考慮都應採用遞進法,其語句頻度為n,成線性增長。需要指出的是,枚舉法思路非常簡單,一一判斷條件是否為空白,再按非空條件搜尋,同時可以利用真值表技術來對付條件極多的情況(相信沒人去幹這種事,4條件時就已經要寫16組語句了);遞進法的思想方法較為巧妙,重在理解,其巧就巧在一是使用了標誌位(flag),二是妙用SQL中字串串連符&。下面以執行個體來講解引擎的建立。

2.執行個體:
我們建立一通訊錄查詢引擎,資料庫名為addressbook.mdb,表名為address,欄位如下:

ID Name Tel School
1 張 三 33333333 電子科技大學電腦系
2 李 四 44444444 四川大學生物系
3 王 二 22222222 西南交通大學建築系
… … … …

Web搜尋介面如下:

姓名: 電話: 學校: 搜尋按鈕

採用枚舉法的來源程式如下:

複製代碼 代碼如下:

<%@ CODEPAGE = "936" %>
'串連資料庫
<%
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頁擷取姓名、電話、學校的值
dim Name
dim Tel
dim School
Name=request("Name")
Tel=request("Tel")
School=request("School")
'枚舉法的搜尋核心,因為有3個條件所以要寫8組If判斷語句
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
'顯示搜尋結果
if rs.eof and rs.bof then
response.write "目前通訊錄中沒有記錄"
else
do while not rs.eof
response.write "姓名:"&rs("Name")&"電話:"&rs("Tel")&"學校:"&rs("School")&"<br>"
rs.movenext
loop
end if
'斷開資料庫
set rs=nothing
conn.close
set conn=nothing
%>

理解上述程式時,著重琢磨核心部分,8組語句一一對應了3個搜尋方塊中的8種狀態

Name Tel School
空 空 空
空 空 非空
空 非空 空
空 非空 非空
非空 空 空
非空 空 非空
非空 非空 空
非空 非空 非空

另外trim()是VB的函數,將輸入的字串前後的空格去掉;%是SQL語言中的多字元萬用字元(_是單字元萬用字元),由此可見%"&trim()&"%對搜尋方塊中輸入的關鍵字是分別向左向右匹配的;SQL語言中用and串連說明非空條件之間是“與”關係。

再來看看遞進法,與枚舉法相比它們只有核心部分不同:
'遞進法的搜尋核心,依次判斷條件為空白否,非空則將其加入搜尋條件

複製代碼 代碼如下:

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

遞進法是一個明智的演算法,單從語句的長短就可以看出來了。這個演算法的痛點和精髓就在flag和&上。首先你應該清楚&在SQL中就是一個字串串連符,把該符號左右的字元拼接在一起。再回到程式,當Name不為空白時sql="select * from address where Name like '%"&Name&"%' "同時flag=1;接下來當Name不為空白時且Tel不為空白時,即Tel<>"" and flag=1時,sql="select * from address where Name like '%"&Name&"%' and Tel like '%"&Tel&"%' "同時flag=1,否則當Name為空白Tel不為空白,sql="select * from address where Tel like '%"&Tel&"%' "同時flag=1;以此類推就可以推廣到n個條件的搜尋。當然條件皆為空白時,即flag=0將選擇所有表中所有項。

3.驗證:

至此,一個搜尋引擎就建立起來了。以下是一些使用樣本:

姓名:張 電話: 學校: 搜尋按鈕

搜尋結果為:
姓名: 張三 電話:33333333 單位:電子科技大學電腦系

姓名: 電話: 學校:大學 搜尋按鈕

搜尋結果為:
姓名:張三 電話:33333333 單位:電子科技大學電腦系
姓名 李 四 電話:44444444 單位:四川大學生物系
姓名:王二 電話:22222222 單位:西南交通大學建築系

姓名: 電話:4444 學校:四川 搜尋按鈕

搜尋結果為:
姓名 李 四 電話:44444444 單位:四川大學生物系

姓名: 電話: 學校:交%大 搜尋按鈕

搜尋結果為:

姓名:王二 電話:22222222 單位:西南交通大學建築系

4.改進:
其實這個引擎還有些缺陷,問題主要在於萬用字元%。一方面是因為人們平時習慣把*作為萬用字元,另一方面%若出現在超連結中,通過request擷取時%將被“吃”掉,如下:

複製代碼 代碼如下:

--test.htm--

<a href=test.asp?content=test%the%sign>click here</a>

--test.asp--
<%
content=request(“content”)
response.write content
%>

在IE中瀏覽test.htm時點擊超連結,顯示為:
testthesign
可見%直接被超連結忽略掉了。怎麼才能解決這個問題呢?很簡單,我們做點小小的手腳--偷梁換柱。
將以下代碼加在搜尋核心之前:
Name=replace(Name,"*","%")
Tel=replace(Tel,"*","%")
Company=replace(Company,"*","%")
將以下代碼加在搜尋核心之後:
Name=replace(Name,"%","*")
Tel=replace(Tel,"%","*")
Company=replace(Company,"%","*")

在我們來分析一下這些語句。replace()是VB中字串替換函數,replace(Name,"*","%") 就是將Name中所有的*換成%。也就是說,我們把3個條件中凡是出現的*都替換為%,這樣一來前3句就將萬用字元改成*了。而後3句就可以防止%被“吃”掉。所有問題就迎刃而解了吧。

姓名: 電話: 學校:交%大 搜尋按鈕

搜尋結果為:
姓名:王 二 電話:22222222 單位:西南交通大學建築系

將上面的語句再改一改,把*用空格代替,不就成了我們在Google、BaiDu中常用的用空格來分開搜尋條件的搜尋引擎了嗎?

補充功能:如果我們要實現查詢同一個表中的標題和內容 但想按這二個的順序來排列 比如 查到與標題符合的先顯示出來 而與內容符合的則顯示在標題的後面 如何?呢? 

sql="select * from product where title like '%"&keyword&"%' "
sql=sql + " union select * from product where content like '%"&keyword&"%' order by id desc"

兩條SQL語句中間加一個,union 就可以聯集查詢,但列必須一樣,還有排序條件也同樣是一個.

我們將用到UNION的聯集查詢,這將能實現以上的功能。

分析:資料庫查詢 將先按與title的資料實現查詢 然後再將實現與content的資料查詢 故則有先後之分。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.