just to add to these answers which helped me out a lot,
i needed to find the immediate parent of a node, as well as the very top level parent of a node chain in some instances,
i used the following as a base to get the items in child-to-parent order
SELECT parent.* FROM
nested_set node,
nested_set parent
WHERE (
node.set_left BETWEEN parent.set_left AND parent.set_right
)
AND node.set_id={CHILD_NODE_ID_HERE}
ORDER BY parent.set_right - parent.set_left
#LIMIT 1,1
it is then a matter of adding the LIMIT 1,1
to only capture the second row which would be the immediate parent
it should also be noted that with the above query if the node itself is the very top level parent, then it would NOT have an immediate parent, so with the LIMIT 1,1
it should return an empty result set
to get the very top level parent i reversed the order by clause, included a check if the node itself is the top parent,and limited the result to the first row
SELECT parent.* AS top_level_right FROM
nested_set node,
nested_set parent
WHERE (
node.set_left >= parent.set_left
AND node.set_left <= parent.set_right
)
AND node.set_id={CHILD_NODE_ID_HERE}
ORDER BY parent.set_left - parent.set_right
LIMIT 1
in the last query i used >= <=
operators so that the selected range encompasses the child node if it also happens to be the top level parent
ORDER BY t2.rgt-t1.rgt
will return an error in sqlite?SQLSTATE[HY000]: General error: 1 no such column: t1.rgt
... it works when i remove t1.rgt from order by so it seems to be an issue with order by... – Questionnaire