濮阳杆衣贸易有限公司

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

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

熱門標(biāo)簽:外呼線路批發(fā) 世界地圖標(biāo)注了哪些城市 地圖標(biāo)注陽江 武穴地圖標(biāo)注 創(chuàng)意電話機(jī)器人 濟(jì)源電銷外呼系統(tǒng)線路 java外呼系統(tǒng)是什么 石家莊慧營銷外呼系統(tǒng) 梧州市地圖標(biāo)注

經(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 多條件符合查詢代碼實(shí)例2

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

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

%
 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ùn)行環(huán)境:IIS
腳本語言:VBScript
數(shù)據(jù)庫:Access/SQL Server
數(shù)據(jù)庫語言:SQL

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

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

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

ID Name Tel School
1 張 三 33333333 電子科技大學(xué)計(jì)算機(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")
'枚舉法的搜索核心,因?yàn)橛?個(gè)條件所以要寫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
%>

理解上述程序時(shí),著重琢磨核心部分,8組語句一一對應(yīng)了3個(gè)搜索框中的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)法是一個(gè)明智的算法,單從語句的長短就可以看出來了。這個(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;接下來當(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;以此類推就可以推廣到n個(gè)條件的搜索。當(dāng)然條件皆為空時(shí),即flag=0將選擇所有表中所有項(xiàng)。

3.驗(yàn)證:

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

姓名:張 電話: 學(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è)引擎還有些缺陷,問題主要在于通配符%。一方面是因?yàn)槿藗兤綍r(shí)習(xí)慣把*作為通配符,另一方面%若出現(xiàn)在超鏈接中,通過request獲取時(shí)%將被“吃”掉,如下:

復(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時(shí)點(diǎn)擊超鏈接,顯示為:
testthesign
可見%直接被超鏈接忽略掉了。怎么才能解決這個(gè)問題呢?很簡單,我們做點(diǎn)小小的手腳--偷梁換柱。
將以下代碼加在搜索核心之前:
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個(gè)條件中凡是出現(xiàn)的*都替換為%,這樣一來前3句就將通配符改成*了。而后3句就可以防止%被“吃”掉。所有問題就迎刃而解了吧。

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

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

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

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

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

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

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

標(biāo)簽:來賓 迪慶 揭陽 淮北 唐山 滁州 甘南 南寧

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP多條件查詢功能實(shí)現(xiàn)代碼(多關(guān)鍵詞查詢)》,本文關(guān)鍵詞  ASP,多,條件,查詢功能,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP多條件查詢功能實(shí)現(xiàn)代碼(多關(guān)鍵詞查詢)》相關(guān)的同類信息!
  • 本頁收集關(guān)于ASP多條件查詢功能實(shí)現(xiàn)代碼(多關(guān)鍵詞查詢)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    边坝县| 平顺县| 柘荣县| 岐山县| 剑阁县| 射阳县| 怀柔区| 安塞县| 辽阳市| 南安市| 夹江县| 铁力市| 响水县| 周口市| 岚皋县| 探索| 昌黎县| 万州区| 万山特区| 镇江市| 祁门县| 澄迈县| 德州市| 合肥市| 宜春市| 吉水县| 平阳县| 和龙市| 玛沁县| 皮山县| 英德市| 淅川县| 宝应县| 九龙城区| 石台县| 云林县| 昆山市| 武汉市| 焉耆| 本溪市| 黄冈市|