0
表名是类别。我如何才能获得所有的categoryIds,直到基础父级?
CategoryId ParentId Name
1 NULL StackOverFlow-1
2 1 StackOverFlow-2
3 1 StackOverFlow-3
4 2 StackOverFlow-4
5 4 StackOverFlow-5
StackOverFlow-5的父级是StackOverFlow-4。
StackOverFlow-4的父级是StackOverFlow-2。
StackOverFlow-2的父级是StackOverFlow-1。
我想打一个函数象下面这样:
GetAllCategoryIdsUntilBaseParentByCategoryId(int Id)
{
//..
}
我认为这应该是一个递归函数。不是吗?
Pseude代码:
int x -> Select ParentId From Category Where Id = 5
int y -> Select ParentId From Category Where Id = x
int z -> Select ParentId From Category Where Id = y
这种模式应该去where ParentId is null
..
我该怎么办呢?