求个递归写法。
现在有 unt_id unt_parent 两个列名
根据已知的 一个unt_id查询 他的下级
unt_parent 副id
无限下级
最后结果想要的是 所有id的一个字符串用逗号分割
------解决方案--------------------http://blog.csdn.net/rubychen410/article/details/4282175
------解决方案--------------------
with cte as
(
select unt_id,unt_parent from tb where unt_id=@unt_id
union all
select a.unt_id,a.unt_parent from tb a join cte b on a.unt_parent=b.unt_id
)
select unt_id+',' from cte for xml path('')
------解决方案--------------------用sql递归
给你个参考
WITH prgs(parent_key, parent_id, child_key, child_id) AS
(
SELECT parent_key, parent_id, child_key, child_id
FROM tablename
WHERE 查询条件
UNION ALL
SELECT e.parent_key, e.parent_id, e.child_key, e.child_id
FROM tablename e
inner join prgs a ON
(a.child_key=e.parent_key AND a.child_id=e.parent_id )
WHERE e.group_id=10
)
select * from prgs