码迷,mamicode.com
首页 > 数据库 > 详细

使用Oracle数据库的递归查询语句生成菜单树

时间:2020-04-26 10:51:25      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:mode   select   执行   ack   each   过滤   arc   tin   led   

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>

 

 

使用Oracle数据库的递归查询语句生成菜单树

标签:mode   select   执行   ack   each   过滤   arc   tin   led   

原文地址:https://www.cnblogs.com/damaoa/p/12777401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!