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