• MYSQL 函数,组织架构树,通过最上级节点id,查出所有多层子节点id


    mysql代码:

    通过父节点查询所有子节点id

    SELECT 
        t3.id
    FROM (
        SELECT 
            t1.id, t1.parentid, 
            t2.*,
            IF(FIND_IN_SET(t1.parentid, @pids) > 0, @pids := CONCAT(@pids, ',', t1.id), 0) AS isChild
        FROM (
            SELECT id, parentid FROM frame_organization WHERE isuse=1 ORDER BY parentid, id
        ) AS t1,
        ( SELECT @pids := 此处写要查询的父节点id ) AS t2
        
    ) t3    
    WHERE t3.isChild != 0

    更多方式详见:https://blog.csdn.net/qq_35206261/article/details/82107127

    通过子节点查询所有父节点id

    SELECT 
        t3.id
    FROM (
     
        SELECT 
            t1.id, t1.parentid, 
            t2.*,
            IF(FIND_IN_SET(t1.id, @pids) > 0, @pids := CONCAT(@pids, ',', t1.parentid), 0) AS isParent
        FROM (
            SELECT id, parentid FROM frame_organization WHERE isuse=1 ORDER BY parentid DESC, id DESC ) t1, 
    (
    SELECT @pids := 指定节点的ID ) t2
    ) t3
    WHERE t3.isParent != 0 AND t3.id != 指定节点的ID

     更多方式详见:https://blog.csdn.net/qq_35206261/article/details/82107127

  • 相关阅读:
    自定义线性表
    网站关键词抓住热点话题获取长尾关键词 给网站增添流量(图)
    Hibernate常见注解说明
    面试问题java基础
    Spring常见的注解说明
    poj3268
    poj3273
    poj3250
    poj3277
    poj3253
  • 原文地址:https://www.cnblogs.com/zyg316/p/13886256.html
Copyright © 2020-2023  润新知