如有表
ID Name ParentID
1 A 0
2 A1 1
3 A2 1
4 A11 2
5 A12 2
6 A21 3
7 A22 3
8 A111 4
生成
A
A1
A11
A111
A12
A2
A21
A22
--------------
然后我现在给我
A2, A111,A21
然后根据上面表找出最高节点(这里不能用Name来对比,我这里这样写是方便理解)
最高节点为:A2(因为他在第二级)
最高公共节点为:A(因为A111和A2,A21总有的最高节为为它)那要得出上面两个值用SQL查询语句要怎么查谢谢
ID Name ParentID
1 A 0
2 A1 1
3 A2 1
4 A11 2
5 A12 2
6 A21 3
7 A22 3
8 A111 4
生成
A
A1
A11
A111
A12
A2
A21
A22
--------------
然后我现在给我
A2, A111,A21
然后根据上面表找出最高节点(这里不能用Name来对比,我这里这样写是方便理解)
最高节点为:A2(因为他在第二级)
最高公共节点为:A(因为A111和A2,A21总有的最高节为为它)那要得出上面两个值用SQL查询语句要怎么查谢谢
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TestTree](
[ID] [int] NULL,
[Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[ParentID] [int] NULL
) ON [PRIMARY]
insert into TestTree select 1,'A',0
insert into TestTree select 2,'A1',1
insert into TestTree select 3,'A2',1
insert into TestTree select 4,'A11',2
insert into TestTree select 5,'A12',2
insert into TestTree select 6,'A21',3
insert into TestTree select 7,'A22',3
insert into TestTree select 8,'A111',4Select * from TestTree
Go
Drop Table TestTree
Go
最高公共节点:根据最高节点的ParentID -1 查询节点名是否包含所有节点名,没有的话继续-1.
最高公共节点:根据最高节点的ParentID -1 查询节点名是否包含所有节点名,没有的话继续-1.
------------------
这样是不行的,上面的数据,只是我比如而已,如果后期我把A2的父级由A改为A111那你这方法就不行了