由父母订购

Ray*_*eng 1 sql oracle

我有一张学生桌:

  • ID号码(8)
  • PARENT_ID号码(8)

有数据:

54, 99
50, null
5, 54,
99, 50
Run Code Online (Sandbox Code Playgroud)

如何按此顺序查询?

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
Run Code Online (Sandbox Code Playgroud)

Mar*_*bak 5

你需要一个CONNECT BY条款.

请参阅SQL小提琴.

希望有所帮助.