Hi,
in oracle we have connect by prior for hierachical data traversal.
in mysql, you can use group_concat  like this :

mysql> select parentid, group_concat(id) from ids
    -> group by parentid;
+----------+------------------+
| parentid | group_concat(id) |
+----------+------------------+
|        0 | 1                |
|        1 | 2,5              |
|        2 | 3,4              |
|        4 | 6                |
+----------+------------------+
4 rows in set (0.13 sec)

unless your need is more complex.

if you use php, you can look at
http://www.sitepoint.com/article/hierarchical-data-database


Mathias

Selon Marcus Bointon <[EMAIL PROTECTED]>:

> I have a table that represents a tree structure via a self-join. I'd
> like to get hold of all parent records in a single query - is such a
> thing possible? e.g. given
>
> id    parentid
> 1    0
> 2    1
> 3    2
> 4    2
> 5    1
> 6    4
>
> If I was starting with record 4, I would want it to return records 2
> and 1 (probably in that order), starting from 5 would just give me 1
> etc. It needs to support arbitrary depth, hence the need for recursion.
>
> Can I do this in one go, or do I have to query iteratively until I
> encounter a zero reference?
>
> Marcus
> --
> Marcus Bointon
> Synchromedia Limited: Putting you in the picture
> [EMAIL PROTECTED] | http://www.synchromedia.co.uk
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]
>
>



-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to