mysql 8 根据parent_id生成全路径 ancestors
create or replace
algorithm = UNDEFINED view `view_dept_ancestors` as with recursive `cte` as (
select
`sys_dept`.`dept_id` as `dept_id`,
`sys_dept`.`parent_id` as `parent_id`,
1 as `lvl`
from
`sys_dept`
union all
select
`c`.`dept_id` as `dept_id`,
`t`.`parent_id` as `parent_id`,
(`c`.`lvl` + 1) as `lvl + 1`
from
(`cte` `c`
join `sys_dept` `t` on
((`t`.`dept_id` = `c`.`parent_id`))))
select
`cte`.`dept_id` as `dept_id`,
group_concat(`cte`.`parent_id` order by `cte`.`lvl` desc separator ',') as `ancestors`
from
`cte`
group by
`cte`.`dept_id`
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。