一种SQL:
select id, level
from tree t
start with t.parent_id = 0
connect by prior t.id = t.parent_id;
select id, level
from tree t
start with t.parent_id = 0
connect by prior t.id = t.parent_id;