分类的sql递归写法

向下递归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:设置全局的递归时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值