So here's the queries I use to grab the information in useful manners.
Grab the Whole Tree:
SELECT *Grabs the Ancestors of a Node:
FROM bills
ORDER BY lft;
SELECT parent.*Grabs the Descendents of a Node:
FROM bills AS node, bills AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.id = ? AND parent.id <> ? (NOTE: Both ?'s are the same value)
ORDER BY node.lft;
SELECT node.*Grab the Parent of a Node:
FROM bills AS node, bills AS parent
WHERE node.lft > parent.lft AND node.rgt < id =" ?">
SELECT parent.*Obviously, the '?' is the ID of the node that you're interested in and you can cater the selected columns to your needs.
FROM bills AS node, bills AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.id = ?
ORDER BY parent.lft DESC LIMIT 1,1
No comments:
Post a Comment