CONNECT BY start with 如何带出根节点的ID

rowland_taft 2009-07-02 07:12:58
create table test1(
IDS number(6),
PARENT_IDS number(6)
);
IDS PARENT_IDS
----------------------------
1 1 0
2 11 1
3 111 11
4 2 0
5 22 2
6 222 22
7 4 9
8 44 4
9 444 44

select c.ids
from test1 c
CONNECT BY PRIOR c.ids = c.PARENT_IDs
start with c.PARENT_IDs in (select ids from test1 where PARENT_IDS = 0)

得到
ids
---
11
111
22
222

想要得到每个id start with的根节点 root_id
想要得到如下的数据
ids root_id
------------
11 1
111 1
22 2
222 2

请教ing...
...全文
386 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hebo2005 2009-07-03
  • 打赏
  • 举报
回复
SYS_CONNECT_BY_ROOT是9I开始有的函数
白发程序猿 2009-07-03
  • 打赏
  • 举报
回复

select ids, substr(path, 2, instr(path, '/', 2) - 2) PARENT_IDs
from (select c.ids, sys_connect_by_path(c.PARENT_IDs, '/') || '/' path
from test1 c
CONNECT BY PRIOR c.ids = c.PARENT_IDs
start with c.PARENT_IDs in
(select ids from test1 where PARENT_IDS = 0))
inthirties 2009-07-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 rowland_taft 的帖子:]
create table test1(
IDS number(6),
PARENT_IDS number(6)
);
        IDS      PARENT_IDS
----------------------------
1 1 0
2 11 1
3 111 11
4 2 0
5 22 2
6 222 22
7 4 9
8 44 4
9 444 44

select c.ids
  from test1 c
CONNECT BY PRIOR c.ids = c.PARENT_IDs
start with c.PARENT_IDs in (select ids from test1 where PARENT_IDS = 0)

得到
ids
---
11
111
22
222

想要得到每个id start w…
[/Quote]

觉得这个字段,最高不应该这样算出来,应该是insert的时候就找好。保存进去就好了。
rowland_taft 2009-07-02
  • 打赏
  • 举报
回复
9i的,10g函数用不了,只要该节点的根节点,继续请教ing...

hebo2005 2009-07-02
  • 打赏
  • 举报
回复
用 SYS_CONNECT_BY_ROOT函数获得
vc555 2009-07-02
  • 打赏
  • 举报
回复
只要根节点,不要它的上级节点信息吗?

3,494

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧