在多个表字段中搜索一个或多个关键字

时间:2016-05-20 11:21:36

标签: ms-access vbscript asp-classic

我在搜索脚本下方在一个表格列中执行关键字搜索。

搜索页面:

<form name="form_6" method="get" action="results_keywords.asp">
<input name="keyword" type="text" placeholder="keyword" size="30">
<input id="submit" type="submit" value="search">
</form>    

结果页面的一部分:

<%
Dim rsKeyword__MMColParam
rsKeyword__MMColParam = "1"
If (Request.QueryString("keyword") <> "") Then 
  rsKeyword__MMColParam = Request.QueryString("keyword")
End If
%>
<%
Dim rsKeyword
Dim rsKeyword_numRows

Set rsKeyword = Server.CreateObject("ADODB.Recordset")
rsKeyword.ActiveConnection = MM_cnKeywords_STRING
rsKeyword.Source = "SELECT * FROM reports WHERE keyword = '" + Replace(rsKeyword__MMColParam, "'", "''") + "' ORDER BY number DESC"
.....

这很好用。 现在我喜欢执行相同的GET搜索字段来搜索10个不同的表列(keyword01,keyword02等)。

如果GET搜索字段可以包含将在上述10个不同的表列中搜索的多个关键字,那将是完美的。 这可能吗?任何帮助/建议都非常感谢。

无法从Access Table切换到SQL数据库。

3 个答案:

答案 0 :(得分:0)

目前还不完全清楚您是要在多列中搜索单个关键字,还是在所有列中搜索所有关键字,或者搜索什么。我选择了下面最简单的选项,即10个输入字段,其中第一个输入的关键字与第一个关键字列相同,第二个字段与第二个列相同,等等。我也不确定这是否是进行AND搜索或OR搜索;我猜到了后者。 (您可以在表单中添加一个字段,供用户选择AND或OR。)

使用查询字符串可能会太笨重。如果发生这种情况,只需将方法更改为&#34; post&#34;并使用Request.Form()代替Request.Querystring()

<form method="get" action="mypage.asp">
<p>Enter keyword(s) to search for:
<%
For i = 1 to 10
    Response.Write "<br>Field " & i & ": <input type='text' name='kw" & i & "' size='15'>"
Next
%>
</p>
<p><input type="submit" value="Search"></p>
</form>

<%
dim i, kw(10), rs, sql

For i = 1 to 10
    kw(i) = Request.Querystring("kw" & i)
    kw(i) = Replace(kw(i),"'","''") '- double up apostrophes
    '- if this is a form for internal use only, you can stop here.
    '- Otherwise, try to clean things up a bit.
    kw(i) = Replace(Replace(kw(i),",",""),";","") '- remove commas and semicolons
    '- etc. - do whatever you can to ensure the inputs are keywords, not sql statements
Next

sql = "SELECT * FROM reports WHERE "
For i = 1 to 10
    If kw(i) <> "" Then
        sql = sql & "(keyword" & Right(100+i,2) & " = '" & kw(i) & "') OR "
        '- that "Right(...)" business is so that you get keyword01, not keyword1
    End If
Next
sql = Left(sql,Len(sql)-4) '- remove last "OR"
sql = sql & " ORDER BY number"
rs = Server.Createobject("ADODB.Recordset")
rs.ActiveConnection = MM_cnKeywords_STRING
rs.Source = sql
...
%>

答案 1 :(得分:0)

看看这是否有帮助。

rsKeyword__MMColParam = "1"
If (Request.QueryString("keyword") <> "") Then

'Assuming keywords entered in the search field are separated by a comma.
'Place keywords in an array
CDArray = Split(Request.QueryString("keyword") , ",")

'Get the number of keywords entered to use for the loops
jMax = ubound(CDArray)
End If

'create a variable to store the search criteria
Dim mysearch

'Use a real field name, e.g, the record id field to use
'as a beginning dummy search criteria. 
'Makes the following loops easier to add/manage.
mysearch = " (afieldname <> '')"


'Loop through the array for each field you want to search.
'In this case, 10 fields.
'Adding the results to the variable.

for j = 0 to jMax
mysearch = mysearch & " OR (fieldname1 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (fieldname2 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (fieldname3 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (fieldname4 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (fieldname5 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (fieldname6 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (fieldname7 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (fieldname8 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (fieldname9 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (fieldname10 LIKE '%" & CDArray(j) & "%')"
next

'Now add the completed search variable to the select string
rsKeyword.Source = "SELECT * FROM reports WHERE" & mysearch & " ORDER BY number DESC"

答案 2 :(得分:0)

@Martha 我的想法是使用包含max的10个表列,使用ONE输入字段执行(多个)关键字搜索。 10个关键字/报告。

@Craig 使用此代码会导致内部服务器错误。我的错误在哪里?

<%
Dim rsKeyword__MMColParam
rsKeyword__MMColParam = "1"
If (Request.QueryString("Keyword") <> "") Then 
CDArray = Split(Request.QueryString("Keyword") , ",")
jMax = ubound(CDArray)
End If
%>
<%
Dim rsKeyword
Dim rsKeyword_numRows
Dim mysearch

mysearch = " (Keyword01 <> '')"

for j = 0 to jMax
mysearch = mysearch & " OR (Keyword01 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (Keyword02 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (Keyword03 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (Keyword04 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (Keyword05 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (Keyword06 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (fieldname07 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (Keyword08 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (Keyword09 LIKE '%" & CDArray(j) & "%')"
next

for j = 0 to jMax
mysearch = mysearch & " OR (Keyword10 LIKE '%" & CDArray(j) & "%')"
next

Set rsKeyword = Server.CreateObject("ADODB.Recordset")
rsKeyword.ActiveConnection = MM_cnKeyword_STRING

rsKeyword.Source = "SELECT * FROM reports WHERE " & mysearch & " ORDER BY number DESC" 
rsKeyword.CursorType = 0
rsKeyword.CursorLocation = 2
rsKeyword.LockType = 1
rsKeyword.Open()

rsKeyword_numRows = 0
%>