Gunakan RECURSIVE CTE
di 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;
Detail dan tautan ke dokumentasi di jawaban saya sebelumnya:
- Apakah PostgreSQL memiliki kolom semu seperti "LEVEL" di Oracle?