Recursive SQL Expression?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi folks...

I've a small problem working out the best method of reading data from a mySQL database with PHP...

The table is a simple self-referential table constructed (loosely) as follows:

cat_id integer unique
cat_name string
cat_parent integer (points to a cat_id or 0 if no parent).

Now, I need to extract the data in 'tree order', that is each cat (catgory) with it's siblings then the next category and so on...

I'm currently forced into using a very nasty bit of code to list each category within a particular category - this is fine for a small dataset, but a large one results in a rather expensive load on the database server.

Anyone got any bright ideas, 'cos I'm stumped.

Assuming the following data:

cat_id cat_name cat_parent
1 SF 0
2 PH 0
3 FO 2
4 BB 1

the sql statement should return a dataset like:

cat_id cat_name cat_parent
2 PH 0
4 BB 2
1 SF 0
3 FO 1

Notice that category names are in order.

This produces the tree-like structure


+--PH
| +--BB
+--SF
+--FO


Thanks for any help...

--
Alan


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux