问题
If I have the following schema & data and am employing the closure table pattern:
+----+----------+------------+--------+
| id | ancestor | descendant | length |
+----+----------+------------+--------+
| 1 | 2 | 2 | 0 |
| 2 | 2 | 12 | 1 |
| 3 | 2 | 13 | 1 |
| 4 | 2 | 14 | 1 |
| 5 | 2 | 15 | 1 |
| 10 | 12 | 12 | 0 |
| 11 | 13 | 13 | 0 |
| 12 | 14 | 14 | 0 |
| 13 | 15 | 15 | 0 |
| 9 | 17 | 20 | 1 |
| 8 | 17 | 19 | 1 |
| 7 | 17 | 18 | 1 |
| 6 | 17 | 17 | 0 |
| 14 | 18 | 18 | 0 |
| 15 | 19 | 19 | 0 |
| 16 | 20 | 20 | 0 |
+----+----------+------------+--------+
What would my join query back to my main table look like to obtain all the sibling rows of row id 2
?
+----+----------+------------+--------+
| id | ancestor | descendant | length |
+----+----------+------------+--------+
| 3 | 2 | 13 | 1 |
| 4 | 2 | 14 | 1 |
| 5 | 2 | 15 | 1 |
+----+----------+------------+--------+
回答1:
The siblings of a given node would have the same ancestor. However, this would include "1" as well as your list:
select t.*
from table t
where t.ancestor = (select ancestor from table t2 where t.id = 2);
In your table, I am not sure what it means for ancestor
to be the same as descendant
. But, I think the following is the query you want:
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;
EDIT:
You can do this as an explicit join like this:
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;
Note: I also added the condition t.id <> 2
so "2" is not considered a sibling of itself.
来源:https://stackoverflow.com/questions/22123187/what-query-would-i-use-to-obtain-sibling-records-when-using-closure-tables