打印

“不太清醒”大哥及各位高手帮忙看看我的查询语句错在那里?

(附上我上次提出的问题:请问,由多个下拉表单构成的查询数据库的系统,怎样才能实现只选其中一至二个下拉表单,其余忽略不选,也能查到正确的数据?现在我只能每个表单都选,才能查到数据......)

我已经修改了代码,现在全部忽略查询和根据性别一项查询已经可以得到正确的结果,但是根据姓和出生年份查询,以及这三项组合查询还是查不出结果,请高手帮忙看看那里有问题?

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="../../Connections/data1.asp" -->
<%
Dim Recordset1__MMColParam
Recordset1__MMColParam = ""
If (Request.Form("select1")   <> "") Then
  Recordset1__MMColParam = Request.Form("select1")
End If
%>
<%
Dim Recordset1__MMColParam1
Recordset1__MMColParam1 = ""
If (Request.Form("select2")   <> "") Then
  Recordset1__MMColParam1 = Request.Form("select2")  
End If
%>
<%
Dim Recordset1__MMColParam2
Recordset1__MMColParam2 = ""
If (Request.Form("select3")   <> "") Then
  Recordset1__MMColParam2 = Request.Form("select3")
End If
%>

<%
set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_data1_STRING
Recordset1.Source ="SELECT * FROM data_table where 1"
if (Recordset1__MMColParam <> "") Then
Recordset1.Source = Recordset1.Source & " and yourname like '" + Replace(Recordset1__MMColParam, "'", "''") +"'"
end if
if (Recordset1__MMColParam1 <> "") Then
Recordset1.Source = Recordset1.Source & " and sex = '" + Replace(Recordset1__MMColParam1, "'", "''") +"'"
end if
if (Recordset1__MMColParam2 <> "") Then
Recordset1.Source = Recordset1.Source & " and brith like '" + Replace(Recordset1__MMColParam2, "'", "''") +"'"
end if
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 3
Recordset1.Open()
Recordset1_numRows = 0
%>
<%
Dim Repeat1__numRows
Repeat1__numRows = 20
Dim Repeat1__index
Repeat1__index = 0
Recordset1_numRows = Recordset1_numRows + Repeat1__numRows
%>
加个trim()吧!

用来去除空格的
不知道你是不是笔误,那个brith对吗?这里是不是拼写错误了?
并没有笔误呀,这几个字段名称就是这样的,我怎么也搞不懂为什么其中一个字段能查询,另外两个就查不了?还有上面cjj所说的加个trim()是什么意思?抱歉我不太明白,能否解释一下?

TOP

还在为头像烦恼?还在为不能关注好友动态烦忧?快来蓝色理想家园吧!
我终于查到问题所在了!

原错误代码为:
if (Recordset1__MMColParam <> "") Then
Recordset1.Source = Recordset1.Source & " and yourname like '" + Replace(Recordset1__MMColParam, "'", "''") +"'"
end if

正确代码应该为:
if (Recordset1__MMColParam <> "") Then
Recordset1.Source = Recordset1.Source & "and yourname like '" + Replace(Recordset1__MMColParam, "'", "''") + "%'"
end if

在最后少了一个“%”,难怪找不到匹配的字段!

TOP