• oracle中的sql用递归查询树格式数据


    1.查询当前节点的子类

    WITH cte_child (
        GRID_ID,
        GRID_NAME,
        PARENT_GRID_CODE
    ) AS (
        --起始条件
        SELECT
            GRID_ID,
            GRID_NAME,
            PARENT_GRID_CODE
        FROM
            T_SG_GRID
        WHERE
            GRID_ID = '2' --列出父节点查询条件
        UNION ALL
            --递归条件
            SELECT
                A .GRID_ID,
                A .GRID_NAME,
                A .PARENT_GRID_CODE
            FROM
                T_SG_GRID A
            INNER JOIN cte_child b ON (
                A .PARENT_GRID_CODE = b.GRID_ID
            )
    ) 
    SELECT
        *
    FROM
        cte_child;

    2.查询当前节点的父类

    WITH cte_child (
        GRID_ID,
        GRID_NAME,
        PARENT_GRID_CODE
    ) AS (
        --起始条件
        SELECT
            GRID_ID,
            GRID_NAME,
            PARENT_GRID_CODE
        FROM
            T_SG_GRID
        WHERE
            GRID_ID = '2' --列出父节点查询条件
        UNION ALL
            --递归条件
            SELECT
                A .GRID_ID,
                A .GRID_NAME,
                A .PARENT_GRID_CODE
            FROM
                T_SG_GRID A
            INNER JOIN cte_child b ON (
                A .GRID_ID = b.PARENT_GRID_CODE
            )
    ) SELECT
        *
    FROM
        cte_child;

    也就是把查询条件换了一个位置而已

    世间种种的诱惑,不惊不扰我清梦
  • 相关阅读:
    2016工作总结与反思
    JSP 标准标签库(JSTL)
    JQuery遍历CheckBox踩坑记
    JAVA中按照""截取字符串
    file上传图片功能
    List转化为Map
    Map转化为List
    对JAVA的LIST进行排序
    根据制定ID查询信息
    制定查询条数
  • 原文地址:https://www.cnblogs.com/javalisong/p/13497728.html
Copyright © 2020-2023  润新知