Multiple Descendants in Family Relationships in Python
In the previous lesson, a father could only have one son. This was achieved by the father having the relationship son_id.
Now let's say a father can have several sons. In this case, we can still make only one table that will be related to itself. Only in the relationship field we will store not id son, but id father. In this case, several users will be able to refer to their father - and thus the father will have several sons:
| id | name | father_id |
|---|---|---|
| 1 | user1 | 3 |
| 2 | user2 | 3 |
| 3 | user3 | 4 |
| 4 | user4 | null |
You can get a user together with his father in the following way:
sqlSELECT
users.name as user_name,
fathers.name as father_name
FROM
users
LEFT JOIN users as fathers ON fathers.id=users.father_id
Задание 1
Let's say we have users. Each user has a father and a mother. Describe the storage structure.
Задание 2
Write a query that will get the user along with his father and mother.