Använd en RECURSIVE CTE
i Postgres:
WITH RECURSIVE cte AS (
SELECT key, value, 1 AS level
FROM taxonomy
WHERE key = 0
UNION ALL
SELECT t.key, t.value, c.level + 1
FROM cte c
JOIN taxonomy t ON t.taxHier = c.key
)
SELECT value
FROM cte
ORDER BY level;
Detaljer och länkar till dokumentation i mitt tidigare svar:
- Har PostgreSQL en pseudo-kolumn som "LEVEL" i Oracle?