SQL SEVER 递归查询


with ts as (
  select fitemclassid,fitemid,fnumber,Fparentid,fname,FFullNumber from t_item where  Fparentid=0 and fitemclassid=4
    --首先要查询出最原始父级的信息
  union all 
    --全连接
  select a.fitemclassid,a.fitemid, a.fnumber,a.Fparentid,a.fname,a.FFullNumber from t_item a inner join ts b on a.Fparentid=b.fitemid
    --将整体的查询结果重新嵌套进语句中,将查询结果集中子类的ID与父类ID关联,进行查询
  )
  select * from ts  order by fnumber

递归查询本质就是:

            1.查询出父级信息;

            2.核心在于在写循环查询体时,外层查询集同时引入进内部,进行迭代;

相关