MYSQL 递归查询示例



-- 单纯使用SQL递归 查询子节点 含自己
-- organization 需要查询的表 id:主键 parent_id:父ID
SELECT
    T3.* 
FROM
    (
    SELECT
        @codes AS _ids,
        ( SELECT @codes := GROUP_CONCAT( id ) FROM organization WHERE FIND_IN_SET( parent_id, @codes ) ) AS T1
    FROM
        organization,
        ( SELECT @codes := 1000093 ) T4 
    WHERE
        @codes IS NOT NULL 
    ) T2,
    organization T3 
WHERE
    FIND_IN_SET( T3.id, T2._ids ) 
ORDER BY
    id;

你可能感兴趣的:(MYSQL 递归查询示例)