Implementing An N-Level Nested Tree In PHP And PostgreSQL, Part 1
Using The Nested Tree Data
There’s a number of things the above data allows us to do.
Selecting the entire tree in order of hierarchy
select * from nested_tree order by nleft
Select a portion of the tree
If we wanted to select everything in the tree within the Links section, we do the following (let’s assume we know Links has an ID of 8)
select nleft, nright from nested_tree where id = 8; /* assume now that nleft and nright values are stored in $nleft and $nright) */ select * from nested_tree where nleft > $nleft and nright < $nright;
The above query will not include the Links node. To include it, change the query to:
select * from nested_tree where nleft >= $nleft and nright <= $nright;
Or you could do all of this in a single query (but you’re performing the same lookup twice so maybe not the most efficient way to do it:
select * from nested_tree where nleft > (select nleft from nested_tree where id = 8) and nright < (select nright from nested_tree where id = 8) order by nleft;
Select all children of a node
We already did this earlier on, using the parent ID. We can also do this using the nlevel value (although the parent ID is probably the easier way)
select nleft, nright, nlevel from nested_tree where id = 8; /* assume now that nleft, nright, nlevel values are stored in $nleft, $nright and $nlevel) */ select * from nested_tree where nleft > $nleft and nright < $nright and nlevel = $nevel + 1;
Find all leaf nodes
This finds all the nodes that have no nodes beneath them:
select * from nested_tree where nright = nleft + 1;
Find a node’s bloodline.
This one is the kicker – you can easily find the full path to a node really easily with this method.
Let’s find the path to the Portals node. Let’s say we already know its ID is 11.
select nleft, nright from nested_tree where id = 11; /* assume now that nleft and nright values are stored in $nleft and $nright) */ select * from nested_tree where nleft <= $nleft and nright >= $nright order by nlevel;
You could also order by nleft – it’ll give you the same results. The results will look something like:
Title ID PARENT ID NLEFT NRIGHT NLEVEL
------------------------------------------------------------------------------
General Resources 1 0 1 22 1
Links 8 1 14 21 2
Portals 11 8 19 20 3The titles won’t be indented – it’s just used for demonstration.
Here’s the bloodline of this article. At time of writing, its nleft was 6 and nright 7.
phpriot=> select title, nleft, nright, nlevel from documents where nleft <= 6 and nright >= 7 order by nlevel;
title | nleft | nright | nlevel
-------------------------------------------------------------------+-------+--------+--------
Development Articles | 3 | 396 | 1
PHP | 4 | 9 | 2
Application Design | 5 | 8 | 3
Implementing an N-Level Nested Tree In PHP and PostgreSQL, Part 1 | 6 | 7 | 4
(4 rows)

