ForeignKey Referencing Same Table

后端 未结 5 1622
栀梦
栀梦 2021-02-05 22:08

There was an interview test in which below was the table and structure

 Table Person = id, name, dob, dod, mother_id, father_id
 Primary Key (id)
 Foreign Key mo         


        
5条回答
  •  旧时难觅i
    2021-02-05 22:35

    This kind of data structure is called a "Self Referencing Table"

    SELECT DISTINCT mothers.*
    FROM person
        inner join person mothers on person.mother_id = mothers.id
    

    and

    SELECT person.*
    FROM person
        inner join person fathers on person.father_id = fathers.id
        inner join person mothers on person.mother_id = mothers.id
    WHERE 
        fathers.name='john smith'
    and 
        mothers.name='jane'
    

提交回复
热议问题