zoukankan      html  css  js  c++  java
  • 通过数据库编程进行递归(Oracle)

    一、用于测试数据及其结构和生成该测试数据的sql语句如下:
    1、测试数据
    nodeId     parentId
    ---------- ----------
    A01        A
    A02        A
    A03        A
    A0101      A01
    A0102      A01
    A0201      A02
    A0202      A02
    B01        B
    B02        B
    B0201      B02
    B0202      B02
    B020101    B0201
    B020102    B0201

    2、建表及生成数据的sql语句
    create table tree
    (
        nodeId varchar2(10) not null,
        parentId varchar2(10) not null
    );

    insert into tree
    select 'A01', 'A' from dual union all
    select 'A02', 'A' from dual union all
    select 'A03', 'A' from dual union all
    select 'A0101', 'A01' from dual union all
    select 'A0102', 'A01' from dual union all
    select 'A0201', 'A02' from dual union all
    select 'A0202', 'A02' from dual union all
    select 'B01', 'B' from dual union all
    select 'B02', 'B' from dual union all
    select 'B0201', 'B02' from dual union all
    select 'B0202', 'B02' from dual union all
    select 'B020101', 'B0201' from dual union all
    select 'B020102', 'B0201' from dual;

    二、递归
    1、递归sql
    select parentId, nodeId
    from tree
    start with parentId = 'B02'
    connect by parentId = prior nodeId
    order by parentId, nodeId;

    2、执行结果
    parentId   nodeId
    B02 B0201
    B02 B0202
    B0201 B020101
    B0201 B020102

    指定parentId时要注意,oracle是区分大小写的。

  • 相关阅读:
    iOS7,iOS8和iOS9的区别
    NSUrl
    什么是 MIME TYPE?
    TCP协议与UDP协议的区别
    KVC、KVO、NSNotification、delegate 总结及区别
    cocoapods 安装过程及常见问题
    素材丶资料
    方法 笔记(二)
    UIWebView UITextView
    在oj中Python的循环输入问题解决
  • 原文地址:https://www.cnblogs.com/aspsmile/p/1260745.html
Copyright © 2011-2022 走看看