Skip to content

Instantly share code, notes, and snippets.

@charlycoste
Last active December 21, 2015 22:59
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 charlycoste/6379320 to your computer and use it in GitHub Desktop.
Save charlycoste/6379320 to your computer and use it in GitHub Desktop.
Fetches all fields of all ancestors for the node 267 (works only with postgresql)
WITH RECURSIVE ezcontentobject_ancestors(node_id, parent_node_id) AS (
SELECT node_id, parent_node_id
FROM ezcontentobject_tree
WHERE parent_node_id > 0
UNION
SELECT parents.node_id, ancestors.parent_node_id
FROM ezcontentobject_tree as parents, ezcontentobject_ancestors as ancestors
WHERE parents.parent_node_id = ancestors.node_id
), ancestors AS (
SELECT * FROM ezcontentobject_ancestors
)
SELECT parents.*
FROM ezcontentobject_tree as parents, ancestors
WHERE parents.node_id = ancestors.parent_node_id and ancestors.node_id = 267
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment