首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > ASP >

读取2个表数据后怎么循环

2012-03-06 
读取2个表数据后如何循环2个表Subject题目和Result答案首先读取Subject的题目SubjectName然后根据Subject

读取2个表数据后如何循环
2个表Subject题目     和     Result答案
          首先读取Subject   的   题目SubjectName   然后   根据   Subject表的题号     SubjectNo     显示Result的匹配的正确答案  
    输出格式:
          题目1  
          答案1
          题目2
          答案2
相关代码:
<table   width= "100% "   height= "69 "     border= "0 "   cellpadding= "0 "   cellspacing= "0 ">   <tr>
<td   height= "69 "> <table   width= "98% "   border= "0 "   align= "center ">
  <tr>
<%   set   Rs=server.createobject( "adodb.Recordset ")  
Rs.open "select   *   from   Subject   where   SortNo= "&TopSortNo& "   order   by   SubjectNo   asc   ",conn,1,1
do   while   not   rs.eof     %>
<td   width= "3% "> <img   src= "images/lt_menu01.gif "   width= "18 "   height= "21 "   /> </td> <td   width= "97% "> <span   class= "hei_12 ">   <input   name= "SubjectNo "   type= "hidden "     value= " <%=rs( "SubjectNo ")%> "   /> </span> &nbsp; <span   class= "hei_12 "> <%=rs( "SubjectName ")%> </span> </td>
</tr>
</table>
<table   width= "98% "   border= "0 "   align= "center ">
<%   num=rs( "SubjectNo ")
set   rs1=Server.CreateObject( "ADODB.RecordSet ")  
Rs1.open "select   *   from   Result   where     SubjectNo= "&num& "     and     TrueFalse=-1   and   SortNo= "&TopSortNo& "     order   by   ResultNo   asc ",conn,1,1
do   while   not   rs1.eof
%>
  <tr> <td   width= "7% "   class= "hei13 "> 答案: </td> <td   width= "93% "> <span   class= "hong12 "> <%=rs1( "ResultName ")%> </span> </td> </tr> </table>
<%   rs.movenext              
loop
rs.close
%>
<%  
rs1.movenext              
loop
rs1.close
%> </td>
</tr>
</table>

[解决办法]
用一个SQL语句就可以实现:Select A.*,B.* from A left join B on A.SubjectNo = B.SubjectNo
[解决办法]
<table width= "100% " height= "69 " border= "0 " cellpadding= "0 " cellspacing= "0 "> <tr>
<td height= "69 "> <table width= "98% " border= "0 " align= "center ">
<tr>
<% set Rs=server.createobject( "adodb.Recordset ")
Rs.open "select * from Subject where SortNo= "&TopSortNo& " order by SubjectNo asc ",conn,1,1
do while not rs.eof %>
<td width= "3% "> <img src= "images/lt_menu01.gif " width= "18 " height= "21 " /> </td> <td width= "97% "> <span class= "hei_12 "> <input name= "SubjectNo " type= "hidden " value= " <%=rs( "SubjectNo ")%> " /> </span> &nbsp; <span class= "hei_12 "> <%=rs( "SubjectName ")%> </span> </td>


</tr>
<%
rs.movenext
loop
%>
</table>
<table width= "98% " border= "0 " align= "center ">
<% num=rs( "SubjectNo ")
set rs1=Server.CreateObject( "ADODB.RecordSet ")
Rs1.open "select * from Result where SubjectNo= "&num& " and TrueFalse=-1 and SortNo= "&TopSortNo& " order by ResultNo asc ",conn,1,1
do while not rs1.eof
%>
<tr> <td width= "7% " class= "hei13 "> 答案: </td> <td width= "93% "> <span class= "hong12 "> <%=rs1( "ResultName ")%> </span> </td> </tr> </table>
<% rs1.movenext
loop
rs.close
rs1.close

set rs=nothing
set rs1=nothing

conn.close
set conn=nothing
%>
</td>
</tr>
</table>

热点排行