Sie tun dies vorzugsweise in Ihrer Anwendungslogik und nicht in Ihrer Abfrage.
Pseudocode
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
}
}
}