子供の名前と祖父の名前を取得するためのクエリを教えてください。
結合を使用して父の名前を簡単に取得できますが、祖父の場合は結合を 2 回行う必要があるため、誰か助けてください。
D.マヘシュ
子供の名前と祖父の名前を取得するためのクエリを教えてください。
結合を使用して父の名前を簡単に取得できますが、祖父の場合は結合を 2 回行う必要があるため、誰か助けてください。
D.マヘシュ
既にあるものと同様の結合を追加するだけです。
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
以下のように単一の結合で可能だと思います-
select t2.fatherid as grandfather
from table1 as t1
inner join table1 as t2 on t1.fatherid=t2.childid
where t1.childid='grandson_id';