时间:2021-07-01 10:21:17 帮助过:13人阅读
select a.[Name] as ‘地区名‘,
b.[Name] as ‘上级地区名‘,
c.[Name] as ‘上上级地区名‘
from tb_TestTreeView as a
left join tb_TestTreeView as b
on a.Parent = b.ID
left join tb_TestTreeView as c
on b.parent=c.id
结果为:
自连接三级(内联接):
select a.[Name] as ‘地区名‘,
b.[Name] as ‘上级地区名‘,
c.[Name] as ‘上上级地区名‘
from tb_TestTreeView as a
inner join tb_TestTreeView as b
on a.Parent = b.ID
inner join tb_TestTreeView as c
on b.parent=c.id
结果为:
自连接四级(左链接):
select a.[Name] as ‘地区名‘,
b.[Name] as ‘上级地区名‘,
c.[Name] as ‘上上级地区名‘,
d.[Name] as ‘上上上级地区名‘
from tb_TestTreeView as a
left join tb_TestTreeView as b
on a.Parent = b.ID
left join tb_TestTreeView as c
on b.parent=c.id
left join tb_TestTreeView as d
on c.parent=d.id
结果为:
自连接四级(内链接):
select a.[Name] as ‘地区名‘,
b.[Name] as ‘上级地区名‘,
c.[Name] as ‘上上级地区名‘,
d.[Name] as ‘上上上级地区名‘
from tb_TestTreeView as a
inner join tb_TestTreeView as b
on a.Parent = b.ID
inner join tb_TestTreeView as c
on b.Parent = c.ID
inner join tb_TestTreeView as d
on c.Parent = d.ID
结果为:
SQL Server之连接
标签:course strong null log 帮助 选择 今天 sel span