比較不錯的asp單表單欄位多條件查詢

來源:互聯網
上載者:User

用途:文章表內文章關鍵字查詢
查詢格式: 百度 google   百度好還是google好 百度+google (也可不輸入條件全部查詢)

<!--#include file="conn.asp"-->
<%
Function SearshSQL(Tname,Lname,Str) 'Tname-表名 Lname-列名 Str-查詢的字元
If Trim(Str)="" then
MySql="Select * From "&Tname
Else
Str=SqlEncode(Str) '預先處理查詢字串
ArrStr=split(Str," ") '用空格分割處理後字串
Umax=Ubound(ArrStr) '分割後數組上維,確定分割成關鍵詞的個數
If Umax>0 Then '關鍵詞中多個空格的處理,從第2維開始處理
For i=1 to Umax
StrTemp=ArrStr(i)
If Trim(StrTemp)="" Then StrTemp="/~~~~~/" '分割成的元素為空白用特殊字元代替
Sql=Sql &" Or "& Lname &" like '%"& StrTemp &"%'"
Next
End If

MySql="Select * From "& Tname &" where "& Lname &" like '%"&ArrStr(0)&"%'"&Sql

Do
j=InStr(1,MySql, "%/~~~~~/%",1)
If j=0 Then
Exit Do
End If
MySql=Replace(MySql," Or "&Lname&" like '%/~~~~~/%'","")
Loop
End IF

SearshSQL=MySql

end function

Function SqlEncode(Str) '格式化字串
str=replace(str," "," ") '先把左右的全形空格替換成半形空格
str=trim(str)
str=replace(str,"["," ")
str=replace(str,"';"," ")
str=replace(str,"'"," ")
str=replace(str,"_"," ")
str=replace(str,"%"," ")
str=replace(str,"+"," ")
str=replace(str," "," ")
sqlencode=str
end function
%>
<%
sql=SearshSQL("article","coments","蘿蔔+白菜 狗熊") +"order by..(排序)"
Set rs=Server.Createobject("ADODB.RECORDSET")
rs.Open sql,conn,1,1
if not rs.eof then

下面的自己寫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.