本文使用Mysql8.0的特新实现递归查询,文中给出了详细的实例代码,话不多说,来一起看看详细的介绍吧。
表结构如下:
递归查找所有上级
with recursive re_temp as ( select id,title,parent_id from product_type where id = 7 union all select t.id,concat(re_temp2.title,'>',t.title),t.parent_id from product_type t inner join re_temp re_temp2 on t.id = re_temp2.parent_id ) select id,title,parent_id from re_temp;
查询结果如下:
递归查找所有下级
with recursive re_temp as ( select id,title,parent_id from product_type where id = 1 union all select t.id,concat(re_temp2.title,'>',t.title),t.parent_id from product_type t join re_temp re_temp2 on t.parent_id = re_temp2.id ) select id, title, parent_id from re_temp
查找结果如下:
0条评论
点击登录参与评论