SELECT*FROM(SELECT@rAS _id,(SELECT@r := parent_id FROM department WHERE id = _id)AS parent_id,@l :=@l+1AS lvl
FROM(SELECT@r :=#{id}, @l := 0) vars,
department h
WHERE@r<>0) T1
JOIN department T2
ON T1._id = T2.id
ORDERBY T1.lvl DESC
查询所有子级
SELECT*FROM(SELECT@idsAS _ids,(SELECT@ids := GROUP_CONCAT( id )FROM department WHERE FIND_IN_SET( parent_id,@ids))AS cids,@l :=@l+1AS LEVEL1
FROM
department,(SELECT@ids :=#{id}, @l := 0 ) bWHERE@idsISNOTNULL) id,
department DATAWHERE
FIND_IN_SET(DATA.id, _ids )ORDERBY
LEVEL1