Need Help to Display Records from 3 Tables
Hi, i hope you all good people there can help me out...here's my problem
i have 4 SQL tables: Topic, Category, Headings and Comments.
each tables has a one-many relationship. ie a Topic can have one or many Category fields, each category field can have non or many headings and each heading can have non or more comments.
Function AddTable()
Dim rs As ADODB.Recordset
Dim SQL As String
Dim heading As Long
rs = OpenRs("select TOP 100 PERCENT StormCategory_1.CategoryName, StormHeading_1.uID, StormHeading_1.Heading, StormHeading_1.Idea from StormCategory_1 INNER JOIN StormHeading_1 on StormCategory_1.uid = StormHeading_1.BelongCategoryID WHERE StormCategory_1.BelongToTopic = '" & topic & "' ORDER BY StormCategory_1.CategoryName")
With rs
Dim oldcategoryname
If Not .EOF Then
Do While Not .EOF
If .Fields("CategoryName").Value <> oldcategoryname Then
oldcategoryname = .Fields("CategoryName").Value
Response.Write("<table id=table2 border=1 width=550 colspan=0 rowspan=0 bgcolor=blue>")
Response.Write("<tr><td><h2>" & .Fields("Categoryname").Value & "</h2>")
Response.Write("<table id=table3 border=1 width=100% colspan=0 rowspan=0 bgcolor=white>")
End If
Response.Write("<tr>")
heading = rs.Fields("uID").Value
Response.Write("<td><a style='color:blue; text-decoration:none' href=idea.aspx?HeadingID=" & heading & ">" & .Fields("Heading").Value & "</a></td></tr>")
.MoveNext()
If .EOF Then
Response.Write("</td></tr></table></table>")
Else
If .Fields("CategoryName").Value <> oldcategoryname Then
Response.Write("</td></tr></table></table>")
End If
End If
Loop
End If
.Close()
AddTableDR = Nothing
End With
SqlConnection2.Close()
End Function
the code above allows me to display records from two tables, ie Category and Heading. but now my problem is i need to add comments to the headings and in turns display the comments under heading.
i would the category, heading to be displayed whether or not there are any comments on them.
is it possible to have a single query and then use nested loops to display the records??
or do i have to use two different sql command???
i hope you all out there can help me out with this problem....
thank you..
|