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

 

posted @ 2020-10-27 17:50  小飛  阅读(843)  评论(0编辑  收藏  举报