IF OBJECT_ID('[Navigation]') IS NOT NULL DROP TABLE [Navigation]
GO
CREATE TABLE Navigation
(
NavID INT PRIMARY KEY NOT NULL,
NavName VARCHAR(50),
ParentID int --为0表示顶级节点
)
INSERT Navigation
SELECT 1,'节点1',0 UNION ALL
SELECT 2,'节点2',0 UNION ALL
SELECT 3,'节点3',0 UNION ALL
SELECT 4,'节点1-1',1 UNION ALL
SELECT 5,'节点4',0 UNION ALL
SELECT 6,'节点3-1',3 UNION ALL
SELECT 7,'节点3-2',3 UNION ALL
SELECT 8,'节点4-1',5 UNION ALL
SELECT 9,'节点1-2',1 UNION ALL
SELECT 10,'节点2-1',2
go
SELECT * FROM Navigation
/*
如何得到如下结果(最好别用存储过程)
NavID NavName ParentID
1 节点1 0
4 节点1-1 1
9 节点1-2 1
2 节点2 0
10 节点2-1 2
3 节点3 0
6 节点3-1 3
7 节点3-2 3
5 节点4 0
8 节点4-1 5
*/
3 回答
皈依舞
TA贡献1851条经验 获得超3个赞
select NavID,NavName ,ParentID from (select NavID,NavName ,ParentID, (case when ParentID = 0 then NavID else ParentID end) as pid from Navigation) a order by pid,navid
- 3 回答
- 0 关注
- 531 浏览
添加回答
举报
0/150
提交
取消