how to query with child relations to same table and order this correctly

Posted by robertpnl on Stack Overflow See other posts from Stack Overflow or by robertpnl
Published on 2010-05-04T08:36:25Z Indexed on 2010/05/04 8:58 UTC
Read the original article Hit count: 253

Filed under:
|

Hi,

Take this table:

id      name      sub_id
---------------------------
1        A        (null)
2        B        (null)
3        A2       1
4        A3       1

The sub_id column is a relation to his owm table, to column ID.

 subid --- 0:1  --- id

Now I have the problem to make a correctly SELECT query to show that the child rows (which sub_id is not null) directly selected under his parent row. So this must be a correctly order:

1    A    (null)
3    A2   1
4    A3   1
2    B    (null)

A normal SELECT order the id. But how or which keyword help me to order this correctly?

JOIN isn't possible I think because I want to get all the rows seperated. Because the rows will be displayed on a Gridview (ASP.Net) with EntityDataSource but the child rows must be displayed directly under his parent.

Thank you.

© Stack Overflow or respective owner

Related posts about sql

Related posts about query