日期:2014-05-16  浏览次数:20432 次

oracle递归查询用法例子
引言:
  有老朋友找我打听oracle递归查询用法,压根没听说过。了解了一下,留个简单例子,方便将来复习。

正文:
1)概念。

  直接度娘“oracle递归查询”,翻到“select * from table_ start with id = 0 connect by  prior pid = id”,看看就明白了。

2)例子。

  笔者在实验这个问题的时候,不巧测试数据库的机子被外星人占领了,说14:00还我们。。。于是来个很歪的写法。也算多个看点吧。

  直接上代码
select t_.a, t_.b, t_.c
  from (
        select 1 as a, 8978 as b, 0 as c from dual
        union select 2, 777, 1 from dual
        union select 3, 666, 2 from dual
        union select 4, 78945, 2 from dual
        union select 5, 1234, 1 from dual
        union select 6, 4678, 1 from dual
        ) t_
-- t_.a = ? 通过修改'?'的值来取,所有a=?的子结点,以及子结点的子结点。
 start with t_.a = 2
connect by prior t_.a = t_.c;
/* output
1	2	777	1
2	3	666	2
3	4	78945	2
*/

  最后一行也可写为
-- 把'='两端的对调过来。表示取a=?为子结点的根结点,以及根结点的根结点。
connect by prior t_.c = t_.a;
/* output
1	2	777	1
2	1	8978	0
*/


引用:
oracle 递归查询