开发者

HTML listing of recordset, resulting from a join on two tables that relate one-many

I have two tables, that relate via a one-to-many relationship i.e

tableOne  (1)----------(*) tableTwo 

Given the basic schema below

tableOne {
    groupID int PK,
    groupTitle varchar
}

and

tableTwo {
    bidID int PK,
    groupID int FK
}

Consider the two tables yield the following record-set based on joining the tables on the tableOne.g开发者_高级运维roupID = tableTwo.groupID,

tableOne.groupID  |  tableOne.groupTitle   | tableTwo.bidID  |  tableTwo.groupID
________________________________________________________________________________ 

         1        |  Physics Group         |      1          |      1 

         2        |  Chemistry Group       |      2          |      2

         2        |  Chemistry Group       |      3          |      2

         1        |  Physics Group         |      4          |      1

I would like to list such a record-set in an HTML table as follows:

tableOne.groupID  |  tableOne.groupTitle   | tableTwo.bidID  |  tableTwo.groupID
________________________________________________________________________________ 

         1        |  Physics Group         |      1          |      1 

                  |  Physics Group         |      4          |      1

         2        |  Chemistry Group       |      2          |      2

                  |  Chemistry Group       |      3          |      2

I'm interested in finding out if this can be done in SQL, or alternatively finding out ways of listing such a record-set in HTML using good standards.

The solution that comes to mind is simply iterating through the record-set and leveraging a sentinel to list all records with the same tableOne.groupID grouped in a single row <tr> - and also listing tableOne.groupIDs once as a unique identifier of that record-group. However I don't want to go down that path as I would like to avoid mixing code with HTML if possible.


You can order the sql results using the ORDER BY clause.

So if you add

ORDER BY tableOne.groupID ASC, tableTwo.bidID ASC

in your query, you are half-way there.

Next step is to loop and print the recordset from your asp page, but also check if the last groupID is different than the current, in order to decide whether to show it or not..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜