我想设计一个存储家庭成员的数据库,并且可以构造一个查询来查找谁是谁的父亲。总之是父子关系。
这就是我想出的
家庭
| id | Name |
---------------------------
| 1 | Ankit |
---------------------------
| 2 | Nishant |
......
为了找到儿子和父亲的关系,我创建了另一个表
父亲
| father_id | Son_id |
--------------------------------
| 1 | 2 |
-------------------------------
.....
我觉得有人可以指导我以及需要编写什么查询来获得这种关系是不正确的。
提前致谢
编辑
好的,我现在尝试查询,但不知何故我收到错误这就是我正在做的
select f.name as father_name, s.name as son_name
from (select family.name from family,father where father.father_id = family.id ) as f Inner Join
(select family.name from family,father where father.son_id = family.id) as s
on
(family.id = father.father_id and family.id = father.son_id)
错误是
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "family.id" could not be bound.
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "father.father_id" could not be bound.
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "family.id" could not be bound.
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "father.son_id" could not be bound.