I have the following table schema:
ID | PARENT ID | NAME
-------------------------
1 | NULL | A - ROOT
2 | 1 | B
3 | 2 | C
4 | 1 | D
5 | 4 | E
6 | 5 | F
The hierarchy look like:
A
-- B
-- -- C
-- D
-- -- E
-- -- -- F
I want to get all child recursively in all descendant levels.
For example when I have A
and query for it, I would like to get back A, B, C, D, E
and F
.
When I have E
I want to get E
and F
.
When I have D
I want to get D, E
and F
.
I am not SQL expert and as a developer normally I would build programmatically loops with DB query and check whether I have children or not and recursively get the children. But this i definitely a very expensive/unperformant approach.
Is there an elegant/better way by using a SQL statement?