Hi all, I guess I found the answer, and thought I should mention it here for any one having the same question:
http://forums.devarticles.com/showpost.php?p=13052&postcount=2 The above solution is javascript-based (that is, you don't need to make any specially formatted or sorted SQL queries -- just throw the unsorted output to the js object, and it will handle the rest). I didn't try it yet with a large number of records, but if anything goes wrong, I'll be here once again complaining :) --- In php_mysql@yahoogroups.com, "agneady" <[EMAIL PROTECTED]> wrote: > > Hi, > > Let's say that I have a table with the following fields: > > id (primary key, integer, auto inc.) > name (varchar) > parentID (int) > > The parentID field is used as a foreign key pointing to a parent > record from the same table. Here is some sample data from the > above-described table: > > *id* *name* *parentID* > ==== ====== ========== > 0001 abcdef 0000 > 0002 defghi 0000 > 0003 qwerty 0001 > 0004 tyuiop 0003 > 0005 tyooou 0001 > 0006 ghjklf 0003 > > What is the most efficient way to query the table to produce a > treeview like the one below? > > abcdef > |--qwerty > |..|--tyuiop > |..|--ghjklf > |--tyooou > defghi > > (hyphens above represent horizontal lines in the tree, while dots = > no line) > > The table is expected to have thousands of records, so execution time > could be an important factor. >