主頁 > 知識庫 > ASP多條件查詢功能實現(xiàn)代碼(多關(guān)鍵詞查詢)

ASP多條件查詢功能實現(xiàn)代碼(多關(guān)鍵詞查詢)

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 銀行業(yè)務(wù) 團(tuán)購網(wǎng)站 電子圍欄 Linux服務(wù)器 服務(wù)器配置 科大訊飛語音識別系統(tǒng) 阿里云

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

復(fù)制代碼 代碼如下:

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,">","gt;")
  kd=replace(kd,"","lt;")
  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的多條件符合查詢語句,自己寫的,拿出來分享一下,絕對原創(chuàng)。請多指教!

復(fù)制代碼 代碼如下:

%
 dim qy
  qy=0
  if request.form("stu_name")>"" then       '第一個條件表單傳遞的數(shù)據(jù)
   str="stu_name='"request.form("stu_name")"'"
   qy=qy+1
  end if

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


運行環(huán)境:IIS
腳本語言:VBScript
數(shù)據(jù)庫:Access/SQL Server
數(shù)據(jù)庫語言:SQL

1.概要:
不論是在論壇,還是新聞系統(tǒng),或是下載系統(tǒng)等動態(tài)網(wǎng)站中,大家經(jīng)常會看到搜索功能:搜索帖子,搜索用戶,搜索軟件(總之搜索關(guān)鍵字)等,本文則是介紹如何建立一個高效實用的,基于ASP的站內(nèi)多值搜索。

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

2.實例:
我們建立一通訊錄查詢引擎,數(shù)據(jù)庫名為addressbook.mdb,表名為address,字段如下:

ID Name Tel School
1 張 三 33333333 電子科技大學(xué)計算機(jī)系
2 李 四 44444444 四川大學(xué)生物系
3 王 二 22222222 西南交通大學(xué)建筑系
… … … …

Web搜索界面如下:

姓名: 電話: 學(xué)校: 搜索按鈕

采用枚舉法的源程序如下:

復(fù)制代碼 代碼如下:

%@ CODEPAGE = "936" %>
'連接數(shù)據(jù)庫
%
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頁獲取姓名、電話、學(xué)校的值
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
'顯示搜索結(jié)果
if rs.eof and rs.bof then
response.write "目前通訊錄中沒有記錄"
else
do while not rs.eof
response.write "姓名:"rs("Name")"電話:"rs("Tel")"學(xué)校:"rs("School")"br>"
rs.movenext
loop
end if
'斷開數(shù)據(jù)庫
set rs=nothing
conn.close
set conn=nothing
%>

理解上述程序時,著重琢磨核心部分,8組語句一一對應(yīng)了3個搜索框中的8種狀態(tài)

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

另外trim()是VB的函數(shù),將輸入的字符串前后的空格去掉;%是SQL語言中的多字符通配符(_是單字符通配符),由此可見%"trim()"%對搜索框中輸入的關(guān)鍵字是分別向左向右匹配的;SQL語言中用and連接說明非空條件之間是“與”關(guān)系。

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

復(fù)制代碼 代碼如下:

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

3.驗證:

至此,一個搜索引擎就建立起來了。以下是一些使用示例:

姓名:張 電話: 學(xué)校: 搜索按鈕

搜索結(jié)果為:
姓名: 張三 電話:33333333 單位:電子科技大學(xué)計算機(jī)系

姓名: 電話: 學(xué)校:大學(xué) 搜索按鈕

搜索結(jié)果為:
姓名:張三 電話:33333333 單位:電子科技大學(xué)計算機(jī)系
姓名 李 四 電話:44444444 單位:四川大學(xué)生物系
姓名:王二 電話:22222222 單位:西南交通大學(xué)建筑系

姓名: 電話:4444 學(xué)校:四川 搜索按鈕

搜索結(jié)果為:
姓名 李 四 電話:44444444 單位:四川大學(xué)生物系

姓名: 電話: 學(xué)校:交%大 搜索按鈕

搜索結(jié)果為:

姓名:王二 電話:22222222 單位:西南交通大學(xué)建筑系

4.改進(jìn):
其實這個引擎還有些缺陷,問題主要在于通配符%。一方面是因為人們平時習(xí)慣把*作為通配符,另一方面%若出現(xiàn)在超鏈接中,通過request獲取時%將被“吃”掉,如下:

復(fù)制代碼 代碼如下:

--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中字符串替換函數(shù),replace(Name,"*","%") 就是將Name中所有的*換成%。也就是說,我們把3個條件中凡是出現(xiàn)的*都替換為%,這樣一來前3句就將通配符改成*了。而后3句就可以防止%被“吃”掉。所有問題就迎刃而解了吧。

姓名: 電話: 學(xué)校:交%大 搜索按鈕

搜索結(jié)果為:
姓名:王 二 電話:22222222 單位:西南交通大學(xué)建筑系

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

補(bǔ)充功能:如果我們要實現(xiàn)查詢同一個表中的標(biāo)題和內(nèi)容 但想按這二個的順序來排列 比如 查到與標(biāo)題符合的先顯示出來 而與內(nèi)容符合的則顯示在標(biāo)題的后面 如何實現(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語句中間加一個,union 就可以聯(lián)合查詢,但列必須一樣,還有排序條件也同樣是一個.

我們將用到UNION的聯(lián)合查詢,這將能實現(xiàn)以上的功能。

分析:數(shù)據(jù)庫查詢 將先按與title的數(shù)據(jù)實現(xiàn)查詢 然后再將實現(xiàn)與content的數(shù)據(jù)查詢 故則有先后之分。

您可能感興趣的文章:
  • 用js實現(xiàn)QQ在線查詢功能
  • asp查詢xml的代碼 不刷新頁面查詢的方法
  • asp.net連接數(shù)據(jù)庫 增加,修改,刪除,查詢代碼
  • asp.net IList查詢數(shù)據(jù)后格式化數(shù)據(jù)再綁定控件
  • asp.net 網(wǎng)頁動態(tài)查詢條件的實現(xiàn)
  • asp.net中g(shù)ridview的查詢、分頁、編輯更新、刪除的實例代碼
  • 基于ASP實現(xiàn)QQ在線查詢功能

標(biāo)簽:棗莊 衡水 廣元 江蘇 蚌埠 大理 衢州 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP多條件查詢功能實現(xiàn)代碼(多關(guān)鍵詞查詢)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266