MySQL递归查询语法
作者:互联网
业务上有一个递归查询数据表进行累加计算的需求,实现方式上有函数、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