Skip to content

Instantly share code, notes, and snippets.

@jonpemby
Created March 3, 2018 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonpemby/d5c5a5c4b4befaef52f832be1cc9ff70 to your computer and use it in GitHub Desktop.
Save jonpemby/d5c5a5c4b4befaef52f832be1cc9ff70 to your computer and use it in GitHub Desktop.
Recursive comments in PostgreSQL
with recursive co (id, parent_id, body) as
(select c.id, c.parent_id, c.body
from comments c
where c.id = 1
union all
select c.id, c.parent_id, c.body
from co
join comments c
on c.parent_id = co.id)
select * from co;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment