Skip to content

Instantly share code, notes, and snippets.

@zeroDivisible
Created November 18, 2013 10:10
Show Gist options
  • Save zeroDivisible/7525530 to your computer and use it in GitHub Desktop.
Save zeroDivisible/7525530 to your computer and use it in GitHub Desktop.
This is just an example pgSQL query. I don't have the exact schema right now, but basically, there was a tag (stored in table tags) which was mapping to parent (using parent_id) tag from the same table. Give a :startingTagId, this query returns a tag and all parents.
with recursive all_tags(rownum, id) as (
select 1::bigint, id, parent_id from tags where id = :startingTagId
union all
select rownum + 1, c.id, c.parent_id
from all_tags a
inner join tags c on a.parent_id = c.id)
select * from all_tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment