sqlserver 查找所有子级

 如上图,我们想查询出所有地区的数据,如果这样查询

select * from dict where parent_id=3

结果:

 ,肯定不是我们想要的所有子级数据

正确查询方式:

with temp as (
    select * from dict where parent_id=3
    union all select dict.* from temp
    inner join dict on temp.id = dict.parent_id
) select * from temp

结果:

,恩香香的

原文地址:https://www.cnblogs.com/Allofus/p/15016333.html