gpt4 book ai didi

mysql - ForeignKey 引用同一个表

转载 作者:IT老高 更新时间:2023-10-29 00:20:06 24 4
gpt4 key购买 nike

有一个面试测试,下面是表格和结构

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

有人问了

  1. "select all who are mothers"
  2. "select those child who are children of 'John Smith' and 'Jane'

我很困惑,因为我假设外键会像往常一样与其他表链接。但那时我失败了。有人知道实际的答案和原因吗?

最佳答案

这种数据结构称为“自引用表”

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

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'

关于mysql - ForeignKey 引用同一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12708579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com