向下递归sql写法
with recursive t1 as(
SELECT * from course_category where id='1'
UNION all
SELECT t2.* from course_category t2 INNER JOIN t1 on t1.id=t2.parentid
)
SELECT * from t1 order by t1.id
向上递归sql写法
with recursive t1 as(
SELECT * from course_category where id='1-1-1'
UNION all
SELECT t2.* from course_category t2 INNER JOIN t1 on t1.parentid=t2.id
)
SELECT * from t1
MySQL为了防止无限递归,默认递归1000次;如果递归超过1000次
限制递归次数/时间:
当出现不符合设置情况的会报错,分为以下几种设置方法:
cte_max_recursion_depth :default 设置为1000,表达递归的层数.可以使用如下语句修改这个值:
SET SESSION cte_max_recursion_depth = 10; -- permit only shallow recursion
SET SESSION cte_max_recursion_depth = 1000000; -- permit deeper recursion
当然也可以设置为global,也就是set global cte_max_recursion_depth = 1000000;这样子就对全局的递归都有限制
max_execution_time :设置最近的递归时间
SET max_execution_time = 1000; -- impose one second timeout
MAX_EXECUTION_TIME:设置全局的递归时间