在等待更新职位时:
SELECT DISTINCT
p.parent AS parent
, c.child AS child
, IFNULL(g.child, NONE ) AS grandchild_of_parent
FROM parent_child as p
LEFT JOIN parent_child AS c ON p.parent = c.parent
LEFT JOIN parent_child AS g ON c.child = g.parent;
Results would look like this:
parent child grandchild_of_parent
1 4 8
1 5 NONE
2 6 9
3 7 10
4 8 11
6 9 NONE
7 10 NONE
8 11 NONE
Such a simple-minded-but-probably-harder-to-maintain type of code, but since I m not familiar with SQL Server 2008 s built in features to handle this type of request, I ll just throw a long shot...
EDIT:
仅此,在你研究<条码>时,你可以看到海因果结果:条码>和(或)<条码>。 ......这将取得你们的成果,但只有1岁和2岁的孙辈才能这样做。
-- A. Parents 1 and 2
SELECT DISTINCT p.parent FROM parent_child AS p
WHERE p.parent IN (1,2)
UNION
-- B : Children of A
SELECT DISTINCT p.child FROM parent_child AS p
WHERE p.parent IN (1,2)
UNION
-- C : Children of B, Grandchildren of A
SELECT DISTINCT p.child FROM parent_child AS p
WHERE p.parent IN (
SELECT DISTINCT p.child FROM parent_child AS p
WHERE p.parent IN (1,2)
)
UNION
-- D : Children of C, Great-Grandchildren of A
SELECT DISTINCT p.child FROM parent_child AS p
WHERE p.parent IN (
SELECT DISTINCT p.child FROM parent_child AS p
WHERE p.parent IN (
SELECT DISTINCT p.child FROM parent_child AS p
WHERE p.parent IN (1,2)
)
)
我再次强烈建议你研究其他人所投的内容,并探讨他们提供的联系。 如果你有重生子女,那么我就不去做最后的长期研究;如果你有重生子女,那将绝对有。