Det gør du helst i din applikationslogik frem for i din forespørgsel.
Pseudokode
query=SELECT id, type_name, type_reportToId FROM table ORDER BY id ASC, report_toId ASC
// loop through rows
currentParent = 0
processed = 0
hierarachyLevel = 0
while ( processed < foundrows )
{
while ( row = fetchrow( dbResult ) )
{
if ( row[type_reportToId] == currentParent )
{
resultHierarchy[hierarchyLevel] = row
currentParent = row[id]
processed++
hierarchyLevel++
dataseek( dbResult, 0 )
break
}
}
}