表结构
sql语句
--SELECT * FROM T_VIPPart with cte as ( SELECT RecID, VIPId,ParentID,MenuName,Content,LinkURL,CreateTime ,Islink from T_VIPPart where ParentID=0 AND IsDeleted=0 AND Status=3 union ALL SELECT s.RecID, s.VIPId,s.ParentID,s.MenuName,s.Content,s.LinkURL,s.CreateTime,s.Islink from cte s inner join T_VIPPart d on s.RecID = d.ParentID ) select * from cte
要求是ParentID=0 是根节点 查询 ParentID=0的子集
应该取【T_VIPPart】表的值。。。
with cte as
(
SELECT RecID, VIPId,ParentID,MenuName,Content,LinkURL,CreateTime ,Islink from T_VIPPart
where ParentID=0 AND IsDeleted=0 AND Status=3
union ALL
SELECT d.RecID, d.VIPId,d.ParentID,d.MenuName,d.Content,d.LinkURL,d.CreateTime,d.Islink
from cte s inner join T_VIPPart d
on s.RecID = d.ParentID
)
select * from cte
我试试等下。谢谢了 在开会 朋友
试了下 ok 还用红色标示 真用心 谢谢
在问个问题 朋友 能排序下吗?
现在是前面3条是跟节点 后面都是不是 希望的结果是 跟节点 子节点 跟 节点 这样的排序?
@s_p: 没什么好办法,可以用拼路径的方式弄一下。。
with cte as
(
SELECT RecID, VIPId,ParentID,MenuName,Content,LinkURL,CreateTime ,Islink ,CAST(CAST(RecID AS VARCHAR(10))+'|' AS VARCHAR(200)) as idpath from T_VIPPart
where ParentID=0 AND IsDeleted=0 AND Status=3
union ALL
SELECT d.RecID, d.VIPId,d.ParentID,d.MenuName,d.Content,d.LinkURL,d.CreateTime,d.Islink ,CAST(s.idpath + CAST(d.RecID AS VARCHAR(10))+'|' AS VARCHAR(200)) AS idpath
from cte s inner join T_VIPPart d
on s.RecID = d.ParentID
)
select RecID, VIPId,ParentID,MenuName,Content,LinkURL,CreateTime ,Islink from cte order by idpath