我想設計一個存儲家庭成員的數據庫,並且可以構建查詢來查找誰是其父親。總之一個父親的兒子關係。父子關係
這是我想出了
家庭
| 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.
爲什麼你覺得這是不正確的?表格本身的關係完全可以。 – 2012-07-19 09:58:10
我有一個想法,它會起作用,但不知道這是做這種事的正確方法,因爲人們可能會進入自己的身份證。一個人不能成爲他自己的父親。所以我有疑問。 – 2012-07-19 11:13:11