根据父id查出所有子级,包括子级的子级,包括自身的id

sys_tenant_company_relation为关联表,
company_id为子id,parent_company_id为父id
SELECT DATA.* FROM(
                              SELECT
                                  @ids as ids,
                                  ( SELECT @ids := GROUP_CONCAT(company_id)
                              FROM sys_tenant_company_relation
                              WHERE FIND_IN_SET(parent_company_id, @ids)
                          ) as cids
            FROM sys_tenant_company_relation
        WHERE @ids IS NOT NULL AND @ids := #{pid}
            ) id, sys_tenant_company_relation DATA
        WHERE FIND_IN_SET(DATA.company_id, ID.ids)

 

posted on 2022-12-07 17:06  wzkris  阅读(2462)  评论(1编辑  收藏  举报