Tag Info

New answers tagged


Obviously, you don't want to count parents, so join the first level of children right away: WITH recursive tree AS ( SELECT p.id AS root_id, c.id, p.content FROM comment p LEFT JOIN comment c ON c.parent_id = p.id WHERE p.parent_id IS NULL UNION ALL SELECT p.root_id, c.id, p.content FROM tree p JOIN comment c ON c.parent_id ...


Your fiddle does not open, so I'm not sure if I understand correctly what you have and what you want, but I'd venture to guess you need to join back to comment on id, something like this: with recursive all_comments as ( select id, parent_id, id as root_id from comment where parent_id is null union all select c.id, c.parent_id, p.root_id ...

Top 50 recent answers are included