发布网友 发布时间:2022-04-08 01:07
共2个回答
懂视网 时间:2022-04-08 05:28
新建测试表
CREATE TABLE [dbo].[test](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[parentId] [bigint] NULL,
[name] [nvarchar](50) NULL,
[jb] [int] NULL,
)
GO
插入测试数据
INSERT INTO test ( Id, parentId, name, jb) values (1,0,‘1‘,1);
INSERT INTO test ( Id, parentId, name, jb) values (2,0,‘2‘,1);
INSERT INTO test ( Id, parentId, name, jb) values (3,1,‘11‘,2);
INSERT INTO test ( Id, parentId, name, jb) values (4,1,‘12‘,2);
INSERT INTO test ( Id, parentId, name, jb) values (5,2,‘21‘,2);
INSERT INTO test ( Id, parentId, name, jb) values (6,2,‘22‘,2);
INSERT INTO test ( Id, parentId, name, jb) values (7,3,‘111‘,3);
INSERT INTO test ( Id, parentId, name, jb) values (8,3,‘112‘,3);
INSERT INTO test ( Id, parentId, name, jb) values (9,5,‘221‘,3);
INSERT INTO test ( Id, parentId, name, jb) values (10,0,‘3‘,1);
树形结构,查询分类下所有子类
;with cte as
(
select * from test where id=1
union all
select a.* from test a join cte b on a.parentId=b.Id
)
select * from cte
sql树形结果,查询所有子类
标签:
热心网友 时间:2022-04-08 02:36
Sql Server 2000因为你这里说了只有*分类,所以我就不写Sql函数了,得到华北下面所有子类别的ClassID(不包括华北的ClassID)select ClassID from Newclass where classParentID in (select ClassID from Newclass where classParentID=51)