⊗pyPmDOSD 126 of 128 menu

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:

users
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:

SELECT users.name as user_name, fathers.name as father_name FROM users LEFT JOIN users as fathers ON fathers.id=users.father_id

Let's say we have users. Each user has a father and a mother. Describe the storage structure.

Write a query that will get the user along with his father and mother.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline