zoukankan      html  css  js  c++  java
  • 使用Oracle数据库的递归查询语句生成菜单树

    SQL 格式

    SELECT * FROM TABLE WHERE [...结果过滤语句]
    START WITH [...递归开始条件]
    CONNECT BY PRIOR [...递归执行条件]

    查询所有下级

    SELECT * FROM TABLE
    START WITH [ID in ('1','2')]
    CONNECT BY PRIOR [ID = ParentId]

     

    查询所有上级

    SELECT * FROM TABLE
    START WITH [ID in ('1','2')]
    CONNECT BY PRIOR [ParentId = ID ]

     

    注意:若出现重复项,可以使用 DISTINCT 进行去重

     

    实例

    <!-- SQL -->
    <select id="getReportGroupTree" resultMap="SysFunctionRm">
        select
        distinct "ID", "FUNCTION_NAME", "ICON", "FUNCTION_URL", "EXPLAINATION", "IS_DISABLED", "PARENT_FUNCTION_ID",
        "ORDER_CODE", "ORDE_RHIERARCHY_CODE", "SYSTEM_ID", "IS_DEFAULT", "IS_NEW_WIN", "FUNCTION_CODE", "FUNCTION_TYPE",
        "PRINT_SCHEME", "FUNCTION_MODE", "IS_PRINT"
        from SYS_FUNCTION WHERE function_Type = 2
        start with
        ID In
        <foreach collection="reportGroupIds" item="id" open="(" close=")" separator="," index="index">
            #{id}
        </foreach>
        connect by prior PARENT_FUNCTION_ID=ID
    </select>

     

  • 相关阅读:
    2-SAT模板
    AC自动机
    省选预备营-Day3(图论) 总结
    省选预备营-Day2(分治) 总结
    左偏树(可并堆)总结
    省选预备营-Day1(数据结构) 总结
    OI基础知识
    C++ 堆
    CH4601 普通平衡树
    java 函数形参传值和传引用的区别
  • 原文地址:https://www.cnblogs.com/damaoa/p/12777401.html
Copyright © 2011-2022 走看看