ForeignKey Referencing Same Table

后端 未结 5 1612
栀梦
栀梦 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条回答
  •  执笔经年
    2021-02-05 22:42

    The answer was already given by podiluska, just explaining how it works since it looks like you're new to MySql.

    By giving an Alias to a table(like mother or father to the table person) you do something like a pseudo-table, that the MySql interprets as another table, so the join happens normally, just imagine there are 3 tables now. Person, Father and Mother, and they are all linked together by the join.

提交回复
热议问题