NYCPHP Meetup

NYPHP.org

[nycphp-talk] Building trees

Steve Manes smanes at magpie.com
Thu Oct 17 07:43:48 EDT 2002


At 01:12 PM 10/16/2002 -0400, Jim Hendricks wrote:
>I know this is a SQL question, but since most apps deal with SQL, I
>figured I'ld ask here since the app is using PHP and MySQL.
>
>I have a table that establishes an entity that may have a parent from
>the same table.  I would like to display the data in a tree format where
>all items with a ParentID of 0 ( no parent ) are listed with children
>indented under the parent.  The nesting can be infinite, but
>realistically will only be 2 or 3 levels deep.  I can see how to do this
>through a whole series of queries, but can see how the performance of
>such a design could be very poor if there is a lot of items.

SQL deals in result sets and is unfortunately not a good tool for working 
with directed graphs, of which trees are one such data structure.  I've 
been working casually on this for a couple of years without much 
success.  There are alternative methods of representing trees in SQL as 
nested sets, or at least it's possible in Oracle.  But it's not 
pretty.  Here's a Joe Celko article I've archived, which is the best I've 
found on the topic of generating trees with SQL:

http://www.dbmsmag.com/9603d06.html






More information about the talk mailing list