ASP多条件クエリ機能の実現コード(多キーワードクエリ)


研究を重ねて下記のコードを書きました。必要なものがあれば、参考にしてください。
ASP多条件はクエリコードのインスタンス2に適合している。
aspの多条件は検索語句に合っています。自分で書いたものを取り出して共有してください。絶対オリジナルです。よろしくお願いします

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
実行環境:IISシナリオ言語:VBIScriptデータベース:Access/SQL Serverデータベース言語:SQL 1.概要:フォーラムであれ、ニュースシステムであれ、ダウンロードシステムであれ、検索機能:検索スレッド、検索ユーザ、検索ソフト(つまり検索キーワード)などがよく見られます。本論文では、ASPベースの駅構内多値検索の効率的かつ実用的な方法を紹介します。
本論文では,「多条件曖昧整合検索」に対して,多条件を理解したが,単一条件検索もこれまでのことである。一般的には、複数の条件検索を行う方法があります。検索条件があまり多くない場合(n<=3)は、エニュメレート・メソッドが使用され、その語句の頻度は2のn乗で指数的に増加し、nは条件数である。条件が高くなると、プログラムの効率から実現可能性まで再進法を採用すべきであり、その語句の頻度はnであり、線形的に増加することは明らかである。指摘したいのは、列挙法の構想はとても簡単で、条件が空かどうかを一つ一つ判断して、非空条件で検索します。同時に、真の値表技術を利用して条件の多い状況に対処できます。漸進法の思想方法はより巧みで、理解に重きを置く。その巧は一つにはマーカービット、二つには妙用SQLの中の文字列コネクタを使っている。エンジンの設立について例を説明します。
2.例:私たちは通信録検索エンジンを作ります。データベース名はaddressbook.mdbで、テーブル名はaddressです。フィールドは以下の通りです。
ID Name Tel School 1枚333333電子科技大学コンピュータ学部2李四44444444四川大学生物学部3王二22222222南西交通大学建築系…
Web検索画面は以下の通りです。
名前:電話:学校:検索ボタン
列挙法を採用したソースプログラムは以下の通りです。

<%
 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
%>
上記の手順を理解する時、核心部分を重点的に検討し、8つの文は一つずつ3つの検索ボックスの8つの状態に対応しています。
Name Tel School空が空いています。非空です。非空です。非空です。非空です。非空です。非空です。非空です。非空です。非空です。非空です。非空です。
またtrim()はVBの関数です。入力した文字列の前後のスペースを削除します。%SQL言語の多文字ワイルドカードです。は1文字のワイルドカードです。したがって、%の"&trim()&"%は検索ボックスに入力されたキーワードを左と右に一致させます。SQL言語では、空でない条件の間に「と」関係があることをand接続で説明します。
再進法を見てみてください。エニュメレーション法と比べてコア部分だけが異なります。

<%@ 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
%>
漸進法は賢明なアルゴリズムであり、語句の長さからもわかる。このアルゴリズムの難点と真髄はflagsと&にあります。まず知っておくべきです。SQLの中には文字列コネクタがあります。この記号の左右の文字をつなぎ合わせます。プログラムに戻ります。Nameが空でない場合はsql=「select*from address where Name like%」&Name&「%」同時にflags=1;続いて、Nameが空でない場合、Telが空でない場合、すなわちTel<>""and flags=1の場合、sql="select*from address where Name like'%"&Name&"%and Tel like'%と同時にframe="この類推によって、n個の条件の検索に普及することができます。もちろん条件が全部空の場合、flags=0はすべての表の中のすべての項目を選択します。
3.検証:
これで一つの検索エンジンが作られました。以下はいくつかの使用例です。
名前:張電話:学校:検索ボタン
検索結果は、名前:張三電話:333333単位:電子科技大学コンピュータ系
名前:電話:学校:大学検索ボタン
検索結果は、名前:張三電話:333333単位:電子科技大学コンピュータ系名前李四電話:44444444単位:四川大学生物系名前:王二電話:2222222222単位:西南交通大学建築系
名前:電話:4444学校:四川検索ボタン
検索結果は、名前李四電話:44444444単位:四川大学生物学部
名前:電話:学校:交際%大検索ボタン
検索結果:
名前:王二電話:222222単位:西南交通大学建築系
4.改良:実はこのエンジンはまだ欠陥があります。問題は主にワイルドカード%にあります。一方、日常的には*をワイルドカードとして使う習慣があるため、一方%がハイパーリンクの中に現れたら、requestを通じて取得した時は%が「食」されます。

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
これらの語句を分析します。replace()はVBの中の文字列の交替の関数で、replace(Name、「*」、「%」)はNameの中のすべての*を%に両替します。つまり、私達は3つの条件の中ですべて現れる*をすべて%に替えて、これで前の3つの文はワイルドカードを*に変えました。そして、3つの文で、%が「食べる」ことを防ぐことができます。すべての問題はすらすらと解決されるだろう。
名前:電話:学校:交際%大検索ボタン
検索結果は、名前:王二電話:222222単位:西南交通大学建築系
上の文を改めて、*をスペースで代用すれば、Google、BaiDuでよく使われるスペースで検索条件を分ける検索エンジンになるのではないでしょうか?
補足機能:同じテーブルのタイトルと内容を調べるために、この二つの順序で並べたいです。例えば、タイトルと一致する先に表示されますが、内容と一致すると、タイトルの後ろに表示されますが、どうやって実現されますか? 
sql=「select*from product where title like%」&keyword&「%」sql=sql+「union select*from product where content like'」&keyword&「%order by desc」
二つのSQL文の中に一つを加えると、unionは共同で調べられますが、列は同じでなければなりません。順序付けの条件も同じです。
私たちはユニオンの共同クエリを利用して、以上の機能を実現します。
分析:データベースの照会は先にtitleとのデータを押して検索を実現してから、contentとのデータ検索を実現します。だから、前後の区別があります。