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

多关联表查询问题,请大家进来帮个忙,不甚感激!

QQ273080973 发布于 2011-07-07 19:45, 393 次点击
我的源代码:[local]1[/local]

数据库lunatic.mdb中的2个关联表有一个相同的字段“Number”,如下:
BigClass:  [local]2[/local]
SmallClass:[local]3[/local]

提交关键字页面index.asp的代码:
程序代码:
<html>
<head>
<title>.............</title>
</head>
<body>
<form action="search.asp" method="post" name="search" target="_blank">
  <p>表BigClass的Big_Title字段
    <input name="Big_Title" type="text" value="" size="20"> </p>
  <p>表SmallClass的Small_Title字段
    <input name="Small_Title" type="text" value="" size="20"></p>
  <p><input type="submit" name="Submit" value="搜索"></p>
</FORM>
</body>
</html>


查询结果页面search.asp的代码:
程序代码:
<%
strConn
= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.mappath("database/lunatic.mdb")+";Persist Security Info=False"
set conn = Server.CreateObject("ADODB.Connection")
conn.open strConn
%>
<html xmlns="http://www.>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<%
  sql
="select * from BigClass where Big_Title like '%"&request("Big_Title")&"%'  "
  
set rs=Server.CreateObject("ADODB.RecordSet")
  rs.open sql,conn,
1,1

 
  
if rs.bof and rs.eof then
  response.Write(
"对不起,没有找到您需要的记录!")
  
else
  
do while not rs.eof
  
%>
<table align="center" width="400" border="0" cellpadding="3" cellspacing="0">
  <tr style="color:#FF0000">
    <td width="200"><%=rs("Number")%></td>
    <td width="200"><%=rs("Big_Title")%></td>
  </tr>
  <%
  sqlt
="select * from SmallClass where Number = '"&rs("Number")&"' and Small_Title like '%"&request("Small_Title")&"%' "
  
set rst=Server.CreateObject("ADODB.RecordSet")
  rst.open sqlt,conn,
1,1

 
  
if rst.bof and rst.eof then
  response.Write(
"<tr><td style='font-size:13px; color:#FF0000'>小类下没有记录!</td></tr>")
  response.Write(
"<tr><td>&nbsp;</td></tr>")
  
else
  
do while not rst.eof
  
%>
  <tr>
    <td><%=rst("Small_Title")%></td>
    <td><%=rst("Small_Name")%></td>
  </tr>
  <%
  rst.movenext
  
loop
  
end if
  rst.close
  
Set rst = Nothing
  
%>
</table>
<hr>
<%
  rs.movenext
  
loop
  
end if
  rs.close
  
Set rs = Nothing
  conn.close
  
set conn=nothing
  
%>
</body>
</html>


我在index.asp页面提交一个关键字“手持终端”进行查询:
[local]4[/local]

查询出来的结果:
[local]5[/local]

请问大家,我如何让“XS004   福建移动公司”这条记录不显示出来,只读取前面的XS001、XS002、XS003这3条记录的“手持终端”的信息。也就是读取的结果如下图:
[local]6[/local]

请问大家怎么实现,谢谢!
2 回复
#2
QQ2730809732011-07-08 14:43
好像图没有上传成功,我再贴一次,大家看看我的想法能否实现哈。

我的源代码:
只有本站会员才能查看附件,请 登录


读取页面代码:
程序代码:
<%
strConn
= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.mappath("database/lunatic.mdb")+";Persist Security Info=False"
set conn = Server.CreateObject("ADODB.Connection")
conn.open strConn
%>
<html xmlns="http://www.>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<%
  sql
="select * from BigClass where Big_Title like '%"&request("Big_Title")&"%'  "
  
set rs=Server.CreateObject("ADODB.RecordSet")
  rs.open sql,conn,
1,1

 
  
if rs.bof and rs.eof then
  response.Write(
"对不起,没有找到您需要的记录!")
  
else
  
do while not rs.eof
  
%>
<table align="center" width="400" border="0" cellpadding="3" cellspacing="0">
  <tr style="color:#FF0000">
    <td width="200"><%=rs("Number")%></td>
    <td width="200"><%=rs("Big_Title")%></td>
  </tr>
  <%
  sqlt
="select * from SmallClass where Number = '"&rs("Number")&"' and Small_Title like '%"&request("Small_Title")&"%' "
  
set rst=Server.CreateObject("ADODB.RecordSet")
  rst.open sqlt,conn,
1,1

 
  
if rst.bof and rst.eof then
  response.Write(
"<tr><td  colspan='2' style='font-size:13px;'>对不起,该大类下的小类没有记录!</td></tr>")
  
else
  
do while not rst.eof
  
%>
  <tr>
    <td><%=rst("Small_Title")%></td>
    <td><%=rst("Small_Name")%></td>
  </tr>
  <%
  rst.movenext
  
loop
  
end if
  rst.close
  
Set rst = Nothing
  
%>
</table>
<hr>
<%
  rs.movenext
  
loop
  
end if
  response.Write(
"总共记录数:"&rs.RecordCount&"")
  rs.close
  
Set rs = Nothing
  conn.close
  
set conn=nothing
%>
</body>
</html>


全部读取的话就是下面的4条记录:
只有本站会员才能查看附件,请 登录


如果按照“配件”来查询的话,结果就是:
只有本站会员才能查看附件,请 登录


我想实现的效果是:
只有本站会员才能查看附件,请 登录


就是说如果大类下面的小类没有记录,那么这条大类也不显示出来,不知道能不能实现。
#3
kombel2011-07-11 14:30
嗯 我学到了
1