my $sth = $dbh->prepare(<<'EOM'); SELECT n.id, n.name, count(*) -1 AS level FROM foo AS n, foo AS p WHERE n.links BETWEEN p.links AND p.rechts GROUP BY n.links ORDER BY n.links EOM $sth->execute; my @levels; while (my $row = $sth->fetchrow_hashref) { my $level = $row->{level}; $levels[$level] = $row->{name}; local $" = " > "; my @ausgabe = (@levels[0 .. $level]); print "@ausgabe\n"; }