if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[superid] int,[tname] varchar(4),[brlevel] int)
insert [tb]
select 1,1,'第一',1 union all
select 2,1,'第二',2 union all
select 3,2,'第三',3select * from [tb]select a.id,a.superid,a.tname,b.tname,a.brlevel
from tb a join tb b
on a.superid=b.id
--测试结果:
/*
id superid tname tname brlevel
----------- ----------- ----- ----- -----------
1 1 第一 第一 1
2 1 第二 第一 2
3 2 第三 第二 3*/
go
create table [tb]([id] int,[superid] int,[tname] varchar(4),[brlevel] int)
insert [tb]
select 1,1,'第一',1 union all
select 2,1,'第二',2 union all
select 3,2,'第三',3select * from [tb]select a.id,a.superid,a.tname,b.tname,a.brlevel
from tb a join tb b
on a.superid=b.id
--测试结果:
/*
id superid tname tname brlevel
----------- ----------- ----- ----- -----------
1 1 第一 第一 1
2 1 第二 第一 2
3 2 第三 第二 3*/
--> (让你望见影子的墙)生成测试数据,时间:2008-12-03
if not object_id('tb') is null
drop table tb
Go
Create table tb([id] int,[superid] int,[tname] nvarchar(2),[brlevel] int)
Insert tb
select 1,1,N'第一',1 union all
select 2,1,N'第二',2 union all
select 3,2,N'第三',3
Go
Select * from tbselect s.id,s.superid,s.tname,supername=t.tname,s.brlevel
from tb s,tb t
where s.superid= t.id1 1 第一 第一 1
2 1 第二 第一 2
3 2 第三 第二 3
declare @table table(id int,superid int,tname nvarchar(8),brlevel int)
insert into @table
select 1,1,N'第一',1
union
select 2,1,N'第二',2
union
select 3,2,N'第三',3select * from @tableselect a.id,a.superid,a.tname,b.tname as supername,a.brlevel
from @table a,@table b
where a.superid=b.brlevel
drop table pro
go
create table pro
(id int,superid int,tname varchar(4), brlevel int)
go
insert into pro
select 1,1,'第一',1 union all
select 2,1,'第二',2 union all
select 3,2,'第三',3
go
select a.id,a.superid,a.tname,b.tname,a.brlevel
from pro as a inner join pro as b
on a.superid=b.id
go
drop table pro
go
create table pro
(id int,superid int,tname varchar(4), brlevel int)
go
insert into pro
select 1,1,'第一',1 union all
select 2,1,'第二',2 union all
select 3,2,'第三',3
go
select * from pro
goselect id, superid,tname,supername=(select tname from pro as b where
b.id=a.superid),brlevel
from pro as a