I have a table that looks like this:
create table rows (
row_key integer primary key,
name varchar(40),
parent integer references rows(row_key)
);
It stores a tree structure, so that this data:
row_key | name | parent
------------------------------------
1 | Level 1 | null
2 | Level 2 - Left | 1
3 | Level 2 - Right | 1
4 | Level 3 - Left | 2
5 | Level 3 - Mid. | 2
6 | Level 3 - Mid. | 3
7 | Level 3 - Right | 3
8 | Level 4 - Left | 4
9 | Level 4 - Mid. | 4
10 | Level 5 - Left | 8
11 | Level 5 - Mid. | 8
means that row_key 1 is the root of the tree, row_keys
2 and 3 are its children, row_keys 4 and 5 are children
of row_key 2, etc.
How do I write a select that will return the
ancestors of a row given the row_key of the row?
In other words, given row_key 10, I want to select
so that I get:
row_key | name | parent
------------------------------------
1 | Level 1 | null
2 | Level 2 - Left | 1
4 | Level 3 - Left | 2
8 | Level 4 - Left | 4
any suggestions? examples?
create table rows (
row_key integer primary key,
name varchar(40),
parent integer references rows(row_key)
);
It stores a tree structure, so that this data:
row_key | name | parent
------------------------------------
1 | Level 1 | null
2 | Level 2 - Left | 1
3 | Level 2 - Right | 1
4 | Level 3 - Left | 2
5 | Level 3 - Mid. | 2
6 | Level 3 - Mid. | 3
7 | Level 3 - Right | 3
8 | Level 4 - Left | 4
9 | Level 4 - Mid. | 4
10 | Level 5 - Left | 8
11 | Level 5 - Mid. | 8
means that row_key 1 is the root of the tree, row_keys
2 and 3 are its children, row_keys 4 and 5 are children
of row_key 2, etc.
How do I write a select that will return the
ancestors of a row given the row_key of the row?
In other words, given row_key 10, I want to select
so that I get:
row_key | name | parent
------------------------------------
1 | Level 1 | null
2 | Level 2 - Left | 1
4 | Level 3 - Left | 2
8 | Level 4 - Left | 4
any suggestions? examples?