由父母订购

时间:2014-07-29 21:27:21

标签: sql oracle

我有一张学生表:

  • ID NUMBER(8)
  • PARENT_ID NUMBER(8)

有数据:

54, 99
50, null
5, 54,
99, 50

如何按此顺序查询?

50 <-- first, because it has no parent, so it's root
99 <-- second, because it's parent is the root item
54 <-- third, because it's parent is the second item
5  <-- forth, because it's parent is the third item

1 个答案:

答案 0 :(得分:5)

您需要CONNECT BY条款。

请参阅SQL Fiddle

希望有所帮助。