Pertama-tama, (2, 'grandparent', null)
harus (3, 'grandparent', null)
jika itu benar-benar kakek-nenek. Kedua, kondisi gabungan (implisit) Anda di bagian rekursif dari kueri Anda mundur, Anda ingin mengeluarkan induk dari rt.levelparent
daripada t.parent_level
:
WITH RECURSIVE recursetree(level_id, levelparent) AS (
SELECT level_id, parent_level
FROM level
WHERE level_id = 197
UNION ALL
SELECT t.level_id, t.parent_level
FROM level t JOIN recursetree rt ON rt.levelparent = t.level_id
-- join condition fixed and ANSI-ified above
)
SELECT * FROM recursetree;