Recursive SQL Expression?
| From: | Alan McFarlane | Date: | Fri, 17 Jan 2003 00:12:40 +0000 |
| Subject: | Recursive SQL Expression? | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-24909@lists.php.net to get a copy of this message | ||
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 stringcat_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 1the sql statement should return a dataset like: cat_id cat_name cat_parent
2 PH 0 4 BB 2 1 SF 0 3 FO 1Notice that category names are in order. This produces the tree-like structure +--PH | +--BB +--SF +--FO Thanks for any help... -- Alan