En el arriba-abajo método, la consulta inicial debe seleccionar solo raíces (elementos sin padres), por lo que la consulta devuelve cada fila solo una vez:
with recursive top_down as (
select id, parent, text
from test
where parent is null
union all
select t.id, t.parent, concat_ws('/', r.text, t.text)
from test t
join top_down r on t.parent = r.id
)
select id, text
from top_down
where id = 4 -- input
Si su objetivo es encontrar un artículo específico, el ascendente enfoque es más eficiente:
with recursive bottom_up as (
select id, parent, text
from test
where id = 4 -- input
union all
select r.id, t.parent, concat_ws('/', t.text, r.text)
from test t
join bottom_up r on r.parent = t.id
)
select id, text
from bottom_up
where parent is null
Elimina las condiciones where finales en ambas consultas para ver la diferencia.
Pruébalo en rextester.