1

子供の名前と祖父の名前を取得するためのクエリを教えてください。

結合を使用して父の名前を簡単に取得できますが、祖父の場合は結合を 2 回行う必要があるため、誰か助けてください。

D.マヘシュ

4

2 に答える 2

6

既にあるものと同様の結合を追加するだけです。

 select grandparent.name, child.name
 from Relationships child
 inner join Relationships parent
  on child.parentid = parent.id
 inner join Relationships grandparent
  on parent.parentid = grandparent.id
于 2010-04-28T06:00:30.580 に答える
0

以下のように単一の結合で可能だと思います-

select t2.fatherid as grandfather 
from table1 as t1 
inner join table1 as t2 on t1.fatherid=t2.childid 
where t1.childid='grandson_id';
于 2010-04-28T06:09:32.313 に答える