我有一个表(MSSQL),其数据如下:
1 AAA 100
2 BBB 101
3 C 100
4 D 100
我想列出像:
100:
- 1 AAA
- 3 C
- 4 D
101:
- 2 BBB
我该怎么做?我在一段时间内完成了一段时间,但结果不是我在等待的事情:
<%
Set Group= TarefasConexaoMSSQL.Execute("SELECT DISTINCT GroupName FROM _NewDummy")
GroupName= Group("GroupName")
WHILE NOT Group.EOF
%>
<p>
<a href="#"><% Response.Write Group("GroupName") %></a>
</p>
<%
Set Itens= TarefasConexaoMSSQL.Execute("SELECT * FROM _NewDummy WHERE GroupName= '"& GroupName &"'")
One= Itens("One")
Two= Itens("Two")
%>
<%
WHILE NOT Itens.EOF
%>
- One: <% Response.Write One %>
- Two: <% Response.Write Two %>
<%
Itens.MOVENEXT
WEND
%>
<%
Group.MOVENEXT
WEND
%>
感谢您提供任何相关信息。
答案 0 :(得分:1)
这里不需要任何嵌套循环,只需按组名排序并跟踪先前的值与当前值。当值更改(或第一个值)显示组名时。
代码:
<%
Set oRS= TarefasConexaoMSSQL.Execute("SELECT * FROM _NewDummy ORDER BY GroupName ASC")
Dim currentGroupName, previousGroupName
currentGroupName = ""
previousGroupName = ""
Do Until oRS.EOF
currentGroupName = oRS("GroupName")
One = oRS("One")
Two = oRS("Two")
If currentGroupName<>previousGroupName Then
Response.Write("<p>")
Response.Write("<a href="#">" & currentGroupName & "</a>")
Response.Write("</p>")
End If
Response.Write("- One: " & One & ", Two: " & Two & "<br />")
previousGroupName = currentGroupName
oRS.MoveNext
Loop
oRS.Close
%>