注册 登录
编程论坛 ASP技术论坛

关于多表查询的问题,请高手进来帮忙,谢谢!

my1127 发布于 2011-06-27 19:45, 335 次点击
[local]1[/local]
附件中是源代码,大家可以下载调试,谢谢!

有两个关联表,我想通过关键字能实现同时在两个表中进行查询,请大家帮忙,谢谢!

数据库:data.mdb

表:sell1、sell2

表sell1字段如下:
[local]2[/local]
表sell2字段如下:
[local]3[/local]

两张表中有一个关联字段:xybh

提交搜索页面index.asp代码如下:

程序代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="Search.asp">
<table width="100%" border="0" cellspacing="2" cellpadding="3">
  <tr>
    <td>合同编号:<input type="text" name="xybhkey" style="width:87px;"></td>
  </tr>
  <tr>
    <td>客户名称:<input type="text" name="mckey" style="width:87px;"></td>
  </tr>
  <tr>
    <td>发货状态:<select name="fhztkey" style="width:87px;">
          <option value="已发货"></option>
          <option value="已发货">已发货</option>
          <option value="未发货">未发货</option>
        </select></td>
  </tr>
  <tr>
    <td>产品名称:<select name="cpmckey" style="width:87px;">
          <option selected="selected"></option>
          <option>设备</option>
          <option>配件</option>
          <option>维修</option>
        </select></td>
  </tr>
  <tr>
    <td> &nbsp;
      &nbsp;&nbsp;&nbsp; <input type="submit" name="Submit" value="搜索" />
    &nbsp; &nbsp; <input type="reset" name="Submit2" value="清除" /></td>
  </tr>
  <tr>
    <td height="15"></td>
  </tr>
</table>
</form>
</body>
</html>

提交页面中有一个文本字段【name="cpmckey"】在表sell2中,其它几个文本字段在表sell1中。

读取搜索结果页面search.asp代码如下:
程序代码:
<%
dim conn
conn
="provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("data.mdb")
set cn=server.createobject("adodb.Connection")
cn.Open conn
%>
<html>
<head>
<title>无标题文档</title>
</head>

<body>
<%
sql
="select * from sell1 where online=Yes "
if request("xybhkey")<>"" then
   sql
=sql + "and xybh like '%"&request("xybhkey")&"%'"
end if
if request("mckey")<>"" then
   sql
=sql + "and mc like '%"&request("mckey")&"%'"
end if
if request("fhztkey")<>"" then
   sql
=sql + "and fhzt like '%"&request("fhztkey")&"%'"
end if
if request("cpmckey")<>"" then     '传送过来的这个字段是不对的,因为这个字段在表sell2中。
   sql=sql + "and cpmc like '%"&request("cpmckey")&"%'"
end if
sql
=sql + " order by id asc"
Set rs= Server.CreateObject("ADODB.Recordset")
rs.open sql, cn,
1, 1

if rs.bof and rs.eof then
response.Write(
"没有找到您需要的记录!")
else
do while not rs.eof
%>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
    <td><%=rs("mc")%></td>
    <td><%=rs("xybh")%></td>
</tr>
  <tr>
    <td>产品名称</td>
    <td>日期</td>
  </tr>
<%
  sql1
="select * from sell2 where xybh like '%"&rs("xybh")&"%' "
  
set rs1=server.CreateObject("adodb.recordset")
  rs1.open sql1,cn,
1,1

 
  
if rs1.bof and rs1.eof then
  response.Write(
"对不起,没有找到您需要的记录!")
  
else
  
do while not rs1.eof
  
%>
  <tr>
    <td><%=rs1("cpmc")%></td>
    <td><%=rs1("date")%></td>
  </tr>
<%
  rs1.movenext
  
loop
  
end if
  rs1.close
  
set rs1=nothing
  
%>
  <tr>
    <td><%=rs("Type")%></td>
    <td><%=rs("fhzt")%></td>
  </tr>
</table>
<hr>
<%
  rs.movenext
  
loop
  
end if
  rs.close
  
set rs=nothing
  cn.close
  
set cn=nothing
  
%>
</body>
</html>

其中关键词【name="cpmckey"】是在表sell2中的。

我的想法是想在index.asp页面能提交2个表sell1、sell2中的字段,然后都能在search.asp中读取出查询的结果出来。

例如:
我在index.asp页面中输入协议编号“P01”
[local]4[/local]
查询出来的结果是:
[local]5[/local]

如果我在index.asp页面中输入产品名称“设备”
[local]6[/local]
查询出来的结果想是:
[local]7[/local]

请问大家怎么实现这个查询功能呢?高手帮忙,谢谢!
0 回复
1