表Test有两列
id
parent_idparent_id为嵌套表结构,对应上级节点ID例: id parent_id
1
2 1
3 1
4 2,3表示如下
1.parent_id=null 根节点
2.parent_id=1 一个父节点
3.parent_id=1 一个父节点
4.parent_id=2,3 两个父节点
实现语句如下:drop table test;create or replace type number_ntabtyp is table of number;create table TEST
(
ID NUMBER,
PARENT_ID NUMBER_NTABTYP
)
nested table PARENT_ID store as NTAB_TEST;insert into test
select id, parent_id
from (select id,
decode(id,
1,
null,
4,
number_ntabtyp(id - 2, id - 1),
number_ntabtyp(id - 1)) parent_id
from (select level id from dual connect by level <= 10));commit;--查询效果如下
select level, sys_connect_by_path(id, '>')
from test
connect by regexp_like((select wm_concat(column_value) from table(parent_id)),
'^([^,]+,)*' || prior to_char(id) || '(,[^,]+)*$')
start with id = 1;
求更高效的查询方式
id
parent_idparent_id为嵌套表结构,对应上级节点ID例: id parent_id
1
2 1
3 1
4 2,3表示如下
1.parent_id=null 根节点
2.parent_id=1 一个父节点
3.parent_id=1 一个父节点
4.parent_id=2,3 两个父节点
实现语句如下:drop table test;create or replace type number_ntabtyp is table of number;create table TEST
(
ID NUMBER,
PARENT_ID NUMBER_NTABTYP
)
nested table PARENT_ID store as NTAB_TEST;insert into test
select id, parent_id
from (select id,
decode(id,
1,
null,
4,
number_ntabtyp(id - 2, id - 1),
number_ntabtyp(id - 1)) parent_id
from (select level id from dual connect by level <= 10));commit;--查询效果如下
select level, sys_connect_by_path(id, '>')
from test
connect by regexp_like((select wm_concat(column_value) from table(parent_id)),
'^([^,]+,)*' || prior to_char(id) || '(,[^,]+)*$')
start with id = 1;
求更高效的查询方式
解决方案 »
- Oracle 插入数据 出现ORA-00942表或视图不存在 请求帮忙
- 一个关于系统sequence资源浪费的问题
- 分组小合计问题
- 高分求oracle sql写法!
- 如何在命令行调用oracle编译工具编译PL/SQL过程? 有谁开发过PL/SQL编辑器,或者提供点思路。
- 从SQL server2000导入数据到Oracle 92出现的问题
- ORA-01461 仅可以为插入 LONG 列的 LONG 值赋值 是什么错误啊
- oracle中在客户端能否用database configuration assistant建库?
- “Oracle JDeveloper”是什么东西?
- 求大神给个linux下安装rac集群详细教程或有这中书的推荐。
- 关于sqlldr导入日期类型的问题
- oracle 存储过程
换个方式描述?