如果用SQL下面的CTE递归处理的话,一次性就能把结果给查询出来,而且性能很不错 比用程序处理(数据量很大的情况),临时表性能更好,更方便 复制代码 代码如下: with area as( select *,id px,cast(id as nvarchar(4000)) px2 from region where parentid=0 union all select a.*,b.px,b.px2+ltrim(a.region_id) from region a join area b on a.parentid=b.id )select * from area order by px,px2
可以查询出结果—-所有分类及相应分类下子分类 id title parentid 1 广东省 0 2 广州 1 3 白云区 2 4 深圳 1 5 湖南省 0 6 长沙 5 7 株洲 5 复制代码 代码如下: with area as( select * from region where parentid=1 union all select a.* from region a join area b on a.parentid=b.id )select * from area
可以查询出结果—-指定分类及相应分类下子分类 id title parentid 1 广东省 0 2 广州 1 3 白云区 2