我不知道你的表结构是什么。如果你将它作为路径存储,那么下面的内容应该可以工作。该查询支持Chris的多条记录。它会选择所有的。
with test as
(
select 1 id, '/King/John/Jack/Chris' str from dual union all
select 2 id, '/King/John/Jack/April' str from dual union all
select 3 id, '/King/John/Jack/Sean' str from dual union all
select 4 id, '/King/Jerry/Tom' str from dual
)
select id,
str,
regexp_substr (str, '[^/]+', 1, rn) split,
rn
from test
cross
join (select rownum rn
from (select max (length (regexp_replace (str, '[^/]+'))) + 1 mx
from test
)
connect by level <= mx
) A
where regexp_substr (str, '[^/]+', 1, rn) is not null
and instr(str, 'Chris') > 0
order by id, rn
;
这里是SQL小提琴为例
Example in SQL Fiddle
诀窍是交叉连接创建用于主表的每一行多行。
ID STR SPLIT RN
1 /King/John/Jack/Chris King 1
1 /King/John/Jack/Chris John 2
1 /King/John/Jack/Chris Jack 3
1 /King/John/Jack/Chris Chris 4
是的,这是可能的。你应该从Chris'开始,选择所有的父母。表格定义和样本数据将会有所帮助。 [我想你可以从这里开始...](http://stackoverflow.com/questions/2319284/sql-recursive-query-on-self-refrencing-table-oracle) – valex