【发布时间】:2014-09-19 11:58:22
【问题描述】:
我需要检索孩子父母的最后一个(或第一个)id。
例子:
ID PARENT_ID
----------------
1 NULL
2 1
3 2
所以如果我搜索 id=3 的父 id,我会得到 1 作为结果。
我试过了,但它给了我相同的 id...
with
tree(id)
as
(
select id
from myTable
where id = 3
union all
select t.id
from myTable t
inner join tree on tree.id = t.father_id
)
select *
from tree;
我已经在这里和几个网站上看到了例子;)
【问题讨论】:
标签: sql sql-server parent-child recursive-query