我试图实现一个类别表。 简化表描述就像是假设这在其父元素之后排序子元素
id -- name -- parent_id
的样本数据,如
id - name - parent_id
1 test1 null
2 test2 null
3 test3 null
4 test4 1
5 test5 4
6 test6 2
7 test7 1
我挣扎拿出一个SQL查询,将返回按以下顺序设置记录
id - name - parent_id
1 test1 null
4 test4 1
5 test5 4
7 test7 1
2 test2 null
6 test6 2
3 test3 null
基本上,子元素在其父元素之后返回。
-----------------------解决方法1:在代码中使用LINQ /递归-------------- -----------
不完全是一个SQL解决方案,但最终它的工作原理。
我不认为有足够的规格。这似乎是任意的,为什么'test1'是'test2'或'test3'之前的父对象。 – 2011-06-08 23:36:48
我可能会错过这一点。但是我看不到样本数据与预期数据顺序之间的关系。 (这是迟到了,所以如果我错过了显而易见的道歉) – lethalMango 2011-06-08 23:37:20
test1应该在测试之前4是他希望根据父亲关系排序,因此test2是有序列表中的下一个ID将是下一个父亲在结果 – Trey 2011-06-08 23:38:19