sql递归查询

例如:找ProductType表 下ID为1的分类的所有子级

with result as    --result为别名
( 
select * from TB_ProductType where Id=1  --查询ID为1 的数据
union all 
select TB_ProductType.* from tb_productType, result where result.ID = TB_ProductType.ParentID  ---递归--找到ID为1的分类的所有下级
) 
 select * from result  --注意 上面只是获取了递归的对象 所以这里必须要查询一次

 

--递归删除  / 更新

with result as 
( 
select * from TB_ProductType where Id=1 
union all 
select TB_ProductType.* from tb_productType, result where result.ID = TB_ProductType.ParentID
) 
update TB_ProductType set name = '' where id in ( select id from result)  -- 

posted @ 2017-05-11 14:38  杨浪  阅读(391)  评论(0)    收藏  举报