Hi Everyone.
I have a database that has a parent/child structure. Some records in the database have a parent record and some a child record and some have both. What I'm looking to do is create two queries, one where I can specify the child and get the parents and one where I specify the parent and get all the child. The database is structured in the following way.
Fields: recordID, parentID, information
Example: 1, 0, some information
Example: 2, 1, some information
Example: 3, 2, some information
Example: 4, 0, some information
Example: 5, 4, some information
Example: 6, 0, some information
So, what I need to do is specify that the record I want is for example recordID = 3, but I also want to know which records are its parents right back to the root parent. So in this case it would return recordID 3, 2 and 1.
I also want a query to go the other way so I could say get recordID = 1 and its children.
At the moment I'm doing this using a loop with a condition statement, but this means doing as many queries as necessary until you reach the root and this can sometimes but 10 or 20. I would like to do it all in one SQL query.
Any ideas?
Best Regards
Andrew.
I have a database that has a parent/child structure. Some records in the database have a parent record and some a child record and some have both. What I'm looking to do is create two queries, one where I can specify the child and get the parents and one where I specify the parent and get all the child. The database is structured in the following way.
Fields: recordID, parentID, information
Example: 1, 0, some information
Example: 2, 1, some information
Example: 3, 2, some information
Example: 4, 0, some information
Example: 5, 4, some information
Example: 6, 0, some information
So, what I need to do is specify that the record I want is for example recordID = 3, but I also want to know which records are its parents right back to the root parent. So in this case it would return recordID 3, 2 and 1.
I also want a query to go the other way so I could say get recordID = 1 and its children.
At the moment I'm doing this using a loop with a condition statement, but this means doing as many queries as necessary until you reach the root and this can sometimes but 10 or 20. I would like to do it all in one SQL query.
Any ideas?
Best Regards
Andrew.