如何在一个循环中循环Recordsets及其子记录

时间:2013-11-11 07:54:00

标签: ms-access asp-classic

我想要循环的帮助,这是我的表格结构&记录在里面。我想要的是循环记录每个U_ID有多个记录具有相同的U_ID

enter image description here

SQL查询

Set Rs2 = con.execute("Select U_ID, stdName, Class_Code, subject, total_marks, act_marks, obt_marks, Format(submit_date,'dd-MMM-yyyy') as submit_date From res_correct ORDER BY submit_date, subject, Class_Code")

我的ASP代码:

If Not RS2.EOF Then

Response.write "<table width=""80%"">"& vbcrlf
Response.write "<tr>"& vbcrlf
Response.write "<td><strong>Name:</strong><span class=""underline"">"& Rs2("stdName")&" </span></td>" & vbcrlf
Response.write "<td><strong>Class:</strong> <span class=""underline"">" & Rs2("Class_Code")&" </span></td>" & vbcrlf
Response.write "<td><strong>UID:</strong> <span class=""underline"">"& Rs2("U_ID") &"</span></td>"& vbcrlf
Response.write "<td><strong>Date:</strong> <span class=""underline"">"& Rs2("submit_date") &"</span></td>"& vbcrlf
Response.write "</tr>" &vbcrlf

Response.write "<tr class=""trhead2"">" &vbcrlf
Response.write "<td>Subject</td>" &vbcrlf
Response.write "<td>Total Marks</td>" &vbcrlf
Response.write "<td>Obtained Marks</td>" &vbcrlf
Response.write "<td>Actual Marks</td>" &vbcrlf
Response.write "</tr>" &vbcrlf
Rs2.MoveFirst

Do
Response.Write "<tr class=""trhighlight"">" &vbcrlf
Response.Write "<td>"& Rs2("Subject") &" </td>" &vbcrlf
Response.Write "<td>"& Rs2("total_marks")&" </td>" &vbcrlf
Response.Write "<td>"& Rs2("obt_marks")&" </td>" &vbcrlf
Response.Write "<td>"& Rs2("act_marks")&" </td>" &vbcrlf
Response.Write "</tr>" &vbcrlf

RS2.MoveNext()

Loop Until RS2.EOF
Response.write "</table>" &vbcrlf
End If

我需要的输出结果如下:

enter image description here

相反,我得到了这个 enter image description here

谢谢!!!

2 个答案:

答案 0 :(得分:3)

将用户信息行包装在条件中,然后在用户更改时计算出来。像这样的东西;

<%
IF RS2.EOF THEN
    'No results
ELSE
    'start loop
    WHILE NOT RS2.EOF
        rowCount = rowCount + 1

        'set the first user and then work out when the user changes
        if userid="" then
            userid=RS2("U_ID")
            progress=true
        else
            if userid-RS2("U_ID") = 0 then
                progress=false
            else
                userid=RS2("U_ID")
                progress=true
            end if
        end if

        IF (progress AND rowCount > 1) THEN
            Response.write "</table>" & vbcrlf
        END IF

        'user has changed
        IF progress THEN
            Response.write "<table width=""80%"">"& vbcrlf
            Response.write "<tr>"& vbcrlf
            Response.write "<td><strong>Name:</strong><span class=""underline"">"& Rs2("stdName")&" </span></td>" & vbcrlf
            Response.write "<td><strong>Class:</strong> <span class=""underline"">" & Rs2("Class_Code")&" </span></td>" & vbcrlf
            Response.write "<td><strong>UID:</strong> <span class=""underline"">"& Rs2("U_ID") &"</span></td>"& vbcrlf
            Response.write "<td><strong>Date:</strong> <span class=""underline"">"& Rs2("submit_date") &"</span></td>"& vbcrlf
            Response.write "</tr>" &vbcrlf

            Response.write "<tr class=""trhead2"">" &vbcrlf
            Response.write "<td>Subject</td>" &vbcrlf
            Response.write "<td>Total Marks</td>" &vbcrlf
            Response.write "<td>Obtained Marks</td>" &vbcrlf
            Response.write "<td>Actual Marks</td>" &vbcrlf
            Response.write "</tr>" &vbcrlf
        END IF

        'write out user's data
        Response.Write "<tr class=""trhighlight"">" &vbcrlf
        Response.Write "<td>"& Rs2("Subject") &" </td>" &vbcrlf
        Response.Write "<td>"& Rs2("total_marks")&" </td>" &vbcrlf
        Response.Write "<td>"& Rs2("obt_marks")&" </td>" &vbcrlf
        Response.Write "<td>"& Rs2("act_marks")&" </td>" &vbcrlf
        Response.Write "</tr>" &vbcrlf

    RS2.MoveNext
    WEND
    IF progress = false THEN
        Response.write "</table>" & vbcrlf
    END IF
END IF
%>

您需要按用户ID订购记录集结果。

答案 1 :(得分:1)

如果我理解正确,我认为你需要两个记录集,一个嵌套在另一个记录集中。

第一个查询 - 我们称之为rs1 - 将类似于

"SELECT uid, studentname, submitdate FROM rescorrect ORDER BY uid"

对于第二个 - 我们称之为rs2 - 将是

"SELECT * FROM rescorrect WHERE uid = " & rs1("uid") & "ORDER BY submitdate, subject, ClassCode"

你也可以看看数据整形 - 真正有用的东西在第3页

http://www.4guysfromrolla.com/webtech/092599-1.shtml