ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MySQL递归查询语法

2022-06-24 09:03:27  阅读:6  来源: 互联网

标签:product codes 递归 T2 语法 MySQL WHERE id SELECT


业务上有一个递归查询数据表进行累加计算的需求,实现方式上有函数、SQL语句等多种方式,最后选择了SQL方式,如下:

    <select id="selectChildren" parameterType="String" resultMap="AccountResult">
        <include refid="xxxx"/>,T2.level
        FROM(
        SELECT @codes as _ids,
        ( SELECT @codes := GROUP_CONCAT(id)
        FROM t_product_account
        WHERE FIND_IN_SET(parent_id, @codes)
        ) as T1,
        @l := @l+1 as level
        FROM t_product_account,
        (SELECT @codes := #{parentId}, @l := -1 ) T4
        WHERE @codes IS NOT NULL
        ) T2, t_product_account a
        WHERE FIND_IN_SET(a.id, T2._ids)
        and id != #{parentId}
        ORDER BY level, id
    </select>

 

标签:product,codes,递归,T2,语法,MySQL,WHERE,id,SELECT
来源: https://www.cnblogs.com/hardrockwu/p/16407513.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有