#最新
Sql技术:mysql根据parent_id生成全路径ancestors

2016-01-26 383

Sql技术: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`

声明: 本站仅提供资源学习下载,资源费用仅为赞助站长的整理费,不代表资源自身价值也不包含任何服务。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
本站提供的资源来自网络,版权争议与本站无关,所有内容及软件的文章仅限用于学习和研究目的。
如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。侵删请致信E-mail:duhaomu@163.com

浩沐资源网 开发技巧 Sql技术:mysql根据parent_id生成全路径ancestors https://www.dhaomu.com/85601.html

相关文章

发表评论
暂无评论
  • 0 +

    访问总数

  • 0 +

    会员总数

  • 0 +

    文章总数

  • 0 +

    今日发布

  • 0 +

    本周发布

  • 0 +

    运行天数

浩沐资源网-打造全网优质免费教程资源分享网站!