根据id向下查询所有自己,包括当前id

    1. SELECT ID.LEVEL,
    2. DATA.*
    3. FROM (
    4. SELECT @ids AS _ids,
    5. (
    6. SELECT @ids := GROUP_CONCAT(id)
    7. FROM 表名
    8. WHERE FIND_IN_SET(父级 id字段, @ids)) AS cids,
    9. @l := @l + 1 AS LEVEL
    10. FROM 表名,
    11. (SELECT @ids := '条件id', @l := 0) b
    12. WHERE @ids IS NOT NULL
    13. ) ID,
    14. 表名 DATA
    15. WHERE FIND_IN_SET(DATA.id, ID._ids)
    16. ORDER BY LEVEL,
    17. id

    实例

    1. SELECT ID.LEVEL,
    2. DATA.*
    3. FROM (
    4. SELECT @ids AS _ids,
    5. (
    6. SELECT @ids := GROUP_CONCAT(id)
    7. FROM org_department_dictionary
    8. WHERE FIND_IN_SET(fatherid, @ids)
    9. ) AS cids,
    10. @l := @l + 1 AS LEVEL
    11. FROM org_department_dictionary,
    12. (SELECT @ids := '1771', @l := 0) b @ids IS NOT NULL
    13. ) ID,
    14. org_department_dictionary DATA
    15. WHERE FIND_IN_SET(DATA.id, ID._ids)
    16. ORDER BY LEVEL,
    17. id