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是区分大小写的。

  • 相关阅读:
    HBase-MapReduce
    HBase API 操 作
    HBase-Shell-数据结构-原理
    HBase-简介-安装配置
    Kafka 与 Flume
    kafka-Streams
    Kafka-producer拦截器(interceptor)
    Kafka-API
    Kafka-工作流程分析
    day06 Java面向对象
  • 原文地址:https://www.cnblogs.com/aspsmile/p/1260745.html
Copyright © 2011-2022 走看看