Verwenden Sie einen RECURSIVE CTE
in 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;
Details und Links zur Dokumentation in meiner vorherigen Antwort:
- Hat PostgreSQL eine Pseudo-Spalte wie "LEVEL" in Oracle?