Brug 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 og links til dokumentation i mit tidligere svar:
- Har PostgreSQL en pseudo-kolonne som "LEVEL" i Oracle?