![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hello, I have the following table: id=int pid=int name=varchar(100) It will hold tree items where 'pid' is the parent id. If zero then the item belongs to the root and has no parents. Suppose I have this structure: [dir] -> pid=0, id=1 dir\f1 -> id=2, pid=1 dir\[dir2] -> id = 3, pid=1 dir\dir2\f2 -> id=4, pid=3 Is there is a nice way to directly figure out the root's id given the record with id=4 ? So I want to tell what is the root parent of itemid=4 (the answer is id=1). Thanks |
#3
| |||
| |||
|
|
Take a look at: http://delicious.com/Captain_Paralytic/hierarchical |
#4
| |||
| |||
|
|
Hello, I have the following table: id=int pid=int name=varchar(100) It will hold tree items where 'pid' is the parent id. If zero then the item belongs to the root and has no parents. Suppose I have this structure: [dir] -> pid=0, id=1 dir\f1 -> id=2, pid=1 dir\[dir2] -> id = 3, pid=1 dir\dir2\f2 -> id=4, pid=3 Is there is a nice way to directly figure out the root's id given the record with id=4 ? So I want to tell what is the root parent of itemid=4 (the answer is id=1). |
#5
| |||
| |||
|
|
Hello, I have the following table: id=int pid=int name=varchar(100) It will hold tree items where 'pid' is the parent id. If zero then the item belongs to the root and has no parents. Suppose I have this structure: [dir] -> pid=0, id=1 dir\f1 -> id=2, pid=1 dir\[dir2] -> id = 3, pid=1 dir\dir2\f2 -> id=4, pid=3 Is there is a nice way to directly figure out the root's id given the record with id=4 ? So I want to tell what is the root parent of itemid=4 (the answer is id=1). |
#6
| |||
| |||
|
|
"lallous" <lallous (AT) lgwm (DOT) org> wrote in message news:09dad895-95dc-4bfd-8810-2c0c5ec16075 (AT) w12g2000yqj (DOT) googlegroups.com... Hello, I have the following table: id=int pid=int name=varchar(100) It will hold tree items where 'pid' is the parent id. If zero then the item belongs to the root and has no parents. Suppose I have this structure: [dir] -> pid=0, id=1 dir\f1 -> id=2, pid=1 dir\[dir2] -> id = 3, pid=1 dir\dir2\f2 -> id=4, pid=3 Is there is a nice way to directly figure out the root's id given the record with id=4 ? So I want to tell what is the root parent of itemid=4 (the answer is id=1). Unless you know the depth in advance I don't think you can generate a single SQL statement which will do it for you. |
|
fall back to a programming language (PHP, Delphi, Java, .NET - whatever you feel most at home with) and do it using that - basically a simple loop reading a record and looping until the record doesn't have a parent. |
#7
| |||
| |||
|
|
Hello, I have the following table: id=int pid=int name=varchar(100) It will hold tree items where 'pid' is the parent id. If zero then the item belongs to the root and has no parents. Suppose I have this structure: [dir] -> pid=0, id=1 dir\f1 -> id=2, pid=1 dir\[dir2] -> id = 3, pid=1 dir\dir2\f2 -> id=4, pid=3 Is there is a nice way to directly figure out the root's id given the record with id=4 ? So I want to tell what is the root parent of itemid=4 (the answer is id=1). Thanks |
#8
| |||
| |||
|
|
Take a look at: http://delicious.com/Captain_Paralytic/hierarchical A warning: I did the left-field / right-field trick and hit myself hard. Especially in combination with autonumbers, the left-right technique, although elegant, causes all kinds of nasty locking problems. These can be worked around, but it was so much trouble that I switched to using path strings instead. Also, a path string can be faster and more easily faster. When you use the left-right approach, you really learn to value the STRAIGT_JOIN keyword in your queries. Best regards, -- Willem Bogaerts Application smith Kratz B.V.http://www.kratz.nl/ |
#9
| |||
| |||
|
|
"lallous" <lallous (AT) lgwm (DOT) org> wrote in message news:09dad895-95dc-4bfd-8810-2c0c5ec16075 (AT) w12g2000yqj (DOT) googlegroups.com... Hello, I have the following table: id=int pid=int name=varchar(100) It will hold tree items where 'pid' is the parent id. If zero then the item belongs to the root and has no parents. Suppose I have this structure: [dir] -> pid=0, id=1 dir\f1 -> id=2, pid=1 dir\[dir2] -> id = 3, pid=1 dir\dir2\f2 -> id=4, pid=3 Is there is a nice way to directly figure out the root's id given the record with id=4 ? So I want to tell what is the root parent of itemid=4 (the answer is id=1). Unless you know the depth in advance I don't think you can generate a single SQL statement which will do it for you. I think you will need to fall back to a programming language (PHP, Delphi, Java, .NET - whatever you feel most at home with) and do it using that - basically a simple loop reading a record and looping until the record doesn't have a parent. |
#10
| |||
| |||
|
|
@Willem: so what exactly you mean by path strings?you store the complete path in each node? |
![]() |
| Thread Tools | |
| Display Modes | |
| |