Let's say there is a table called people with the following columns:
- person_id (integer)
- parent_person_id (integer)
- name (varchar)
Now let's say that the table is populated, and the values ββof the names are just letters (A, B, C, D ...). Looking at each parent, we end up with a hierarchical tree similar to one. Values ββare in the format:
person_id, parent_person_id, name
Consider the above structure of A and B as the first generation, C and E as the second generation, D and F as the third generation, and G and H as the fourth generation. For first-generation elements, parent_person_id is equal to person_id of the element.
, (, , , ..) . . :
1st Generation
person name | parent name
A | A
B | B
person name | parent name
C | B
E | B
3-
person name | parent name
D | C
F | E
4-
person name | parent name
G | F
H | F
, , .
SELECT
child.name as 'person name',
parent.name as 'parent name'
FROM
people as child
JOIN
people as parent
ON
child.parent_person_id = parent.person_id
WHERE
;
- , ? .