【发布时间】:2010-12-08 22:26:09
【问题描述】:
id parent_id
1 0
2 0
3 2
4 0
5 1
6 0
我需要一个返回父行 (parent_id=0) 后跟其子行的查询:
- 第一父母
- 第一父母的所有孩子
- 第二个父母
- 第二个父母的所有孩子
- 第三父母
- 第四个父母
预期结果:按 id 排序
id parent_id
-------------------------------------------
1 0 (first parent)
5 1 (all children of first parent)
2 0 second parent
3 2 (all children of second parent)
4 0 third parent
6 0 fourth parent
我可以使用父母的联合,然后是所有孩子 但这给了我父母,然后是孩子。 我需要父母和立即的孩子。
有人可以帮忙吗?
【问题讨论】:
标签: sql hierarchical