特定のノードの兄弟は同じ祖先を持ちます。ただし、これには「1」とリストが含まれます:
select t.*
from table t
where t.ancestor = (select ancestor from table t2 where t.id = 2);
あなたのテーブルでは、ancestor
にとってそれが何を意味するのかわかりません descendant
と同じになります 。しかし、私は以下があなたが望むクエリだと思います:
select t.*
from table t
where t.ancestor = (select ancestor from table t2 where t2.id = 2) and
t.ancestor <> t.descendant and
t.id <> 2;
編集:
これは明示的として実行できます このように参加します:
select t.*
from table t join
table t2
on t.ancestor = t2.ancestor and
t2.id = 2 a
where t.id <> 2 and
t.ancestor <> t.descendant;
注:条件t.id <> 2
も追加しました したがって、「2」はそれ自体の兄弟とは見なされません。