2012-12-19 23 views
0

我正在使用经典ASP从SQL Server 2000数据库提取记录。这是我用过的代码。带有AND和OR运算符的SQL select语句问题

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语句将按预期,并显示其中包含类似货号多个记录。 当我在第一个输入框中输入目录号,并在另一个输入框中输入批号时,问题会增加。我希望的输出应该只是一个单独的记录,因为我已经给出了产品编号批号,但它显示多个记录。

我提供的图像更清晰。

  1. 在这种图像下面我已经把为产品编号所以它显示两个条目。 enter image description here

  2. 在此图像中我已经把为产品编号和的批号,也显示了两个条目,而不是1,这是什么问题,应该表现出一个条目因为批号是唯一的,而猫号可以是相同的。 enter image description here

+0

尝试'AND'而不是'OR' .. –

回答

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 

我希望这能帮助

+0

不完全是我想要的,谢谢您的回复。仍然在寻找解决方法。@ naoki –

+0

非常感谢您的帮助 –

2

看了你的问题你自己,想想你在说:

幽州“我的期望输出应该有显示已经只是一个单一的记录,因为我已经给货号和批号”

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

关键词是AND与或

在你的想法中,你认为这两个条件必须是真实的,但你的查询否则说。如果你想要两个条件都是真的,那么你必须使用AND运算符。

Naoki正在做的事 - 你将不得不根据指定的标准来修改你的SQL。

+0

感谢您的回复,但仍然对如何实施它感到困惑。 –