Skip to content

Instantly share code, notes, and snippets.

@szymonk92
Created July 30, 2019 09:32
Show Gist options
  • Save szymonk92/b5f0d2e179528c81497e77e99857c946 to your computer and use it in GitHub Desktop.
Save szymonk92/b5f0d2e179528c81497e77e99857c946 to your computer and use it in GitHub Desktop.
Get all ancestors
WITH RECURSIVE ancestors(id, name) AS (
SELECT entity.id, entity.name
FROM t_directory as entity
WHERE entity.parentDirectory = '201'
UNION ALL
SELECT next.id, next.name
FROM t_directory as next, ancestors
WHERE next.parentDirectory = ancestors.id
)
SELECT * FROM ancestors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment