Det gör du helst i din applikationslogik snarare än i din fråga.
Pseudokod
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
}
}
}