create table #([ID] int,[name] nvarchar(10),pID int)
insert into #
select 1,'类一',0 union all
select 2,'类二',0 union all
select 3,'项一',1 union all
select 4,'项二',1 union all
select 5,'项三',2 union all
select 6,'项四',2 union all
select 7,'类三',0 union all
select 8,'项五',7 union all
select 9,'项六',1
select A.* from # A left join # B on a.pID = b.[ID]
order by isnull(B.[ID],A.[ID]),A.[ID]
本文介绍了一个SQL表的创建方法及数据插入过程,并通过一个左连接查询示例展示了如何从表中检索数据,包括不同层级的数据关联。
4269

被折叠的 条评论
为什么被折叠?



