0

我正在使用 Classic ASP 从 SQL Server 2000 数据库中获取记录。这就是我使用的代码。

ASP 代码:

<form name="search" method="post" onsubmit="return attention();">
<table width="60%" border="0" cellpadding="2" cellspacing="0" style="margin:0 auto">
<tr>
    <td colspan="2"><h1 style="color:#003399;">Search for Certificate of Analysis</h1></td>
</tr>
<tr>
    <td valign="bottom">Product number <sup style="color:#EE0000;font-size:1.3em">*</sup></td>
    <td valign="bottom">Lot number</td>
</tr>
<tr>
    <td width="30%" valign="top">
        <input type="text" name="input1" size="20"/>
    </td>
    <td valign="top">
        <input type="text" size="20" name="input2"/> 
        <input style="background-color:#ffffff;border:0px;cursor:pointer" size="10" type="submit" name="sub" value=">>" />
    </td>
</tr>

</table>
<%
    if request.Form("sub") <> "" then
    Dim fname, lname
    fname= request.Form("input1")
    lname= request.Form("input2")
    session("n") = fname & lname
    sql = "Select * from search where cat_no_batch_no LIKE '"&request.Form("input1")&"%' OR cat_no_batch_no='"&session("n")&"'"
    rs.open sql, con, 1, 2
    if rs.eof then

    response.write("Please provide a valid Cat No.")    

    else



            do while not rs.eof
        %>
            <table border="1" cellpadding="5" cellspacing="0" width="60%" style="margin:0 auto; border-collapse:collapse;border-color:#999999">
            <tr>
                <td width="50%"><%=rs("cat_no_batch_no")%></td>
                <td width="10%">Click to open <a target="_blank" href="http://localhost/search1/<%=rs("pdf_path")%>"><%=rs("cat_no_batch_no")%></a></td>
            </tr>
            </table>

        <%
            rs.movenext
            loop
        end if
    rs.close
    end if
%>
</form>

上面的 LIKE 语句按预期工作,并显示包含类似 Cat No.的多条记录。当我在第一个输入框中输入Cat No并在另一个输入框中输入批号时,问题就出现了。我想要的输出应该只是显示一条记录,因为我已经给出了Cat No.Lot Number,但它显示了多条记录。

我提供了更清晰的图像。

  1. 在下图中,我将6106021作为产品编号,因此它显示了两个条目。 在此处输入图像描述

  2. 在这张图片中,我将6106021作为产品编号,将218111作为批号,它显示两个条目而不是 1。这就是问题所在,它应该显示一个条目,因为批号是唯一的,而猫号可以相同。 在此处输入图像描述

4

2 回答 2

2

把你的问题读给自己听,想想你在说什么:

您声明“我想要的输出应该只是一条要显示的记录,因为我已经给出了 Cat No. 和 Lot Number”

但是在你的 SQL 中你写where cat_no_batch_no LIKE '"&request.Form("input1")&"%' OR cat_no_batch_no='"&session("n")&"'

关键词是 AND vs. OR

在您的脑海中,您认为这两个条件都必须为真,但您的查询却不然。如果您希望这两个条件都为真,那么您必须使用 AND 运算符。

Naoki 正在做一些事情——您将不得不根据指定的条件修改您的 SQL。

于 2012-12-19T06:03:53.687 回答
1

我想您可以根据是否提供“批号”来编写两个单独的 SQL 查询。您的代码可能如下所示:

if lname = "" then
    sql = "Select * from search where cat_no_batch_no LIKE '"&request.Form("input1")&"%'" 
else
    sql = "Select * from search where cat_no_batch_no LIKE '"&session("n")&"%'"
end if

我希望这会有所帮助

于 2012-12-19T05:46:02.150 回答