34,588
社区成员
发帖
与我相关
我的任务
分享
create table #tmp(id int,pid varchar(30),Context varchar(30))
insert into #tmp
select 1, null , '1' union all
select 2, null , '2' union all
select 3, null , '3' union all
select 4, null , '4' union all
select 5, 1 , '1_1' union all
select 6, 2 , '2_1' union all
select 6, 3 , '3_1' union all
select 7, 4 , '4_1'
--下面这句显然错误
select B.id,B.Context,(select Context from #tmp as A where A.pid=B.id )as pContext from #tmp as B
DROP TABLE #tmp
id Context pContext
1 1 1_1
2 2 2_1
3 3 3_1
4 4 4_1
5 1_1 NULL
6 2_1 NULL
6 3_1 NULL
7 4_1 NULLcreate table #tmp(id int,pid varchar(30),Context varchar(30))
insert into #tmp
select 1, null , '1' union all
select 2, null , '2' union all
select 3, null , '3' union all
select 4, null , '4' union all
select 5, 1 , '1_1' union all
select 6, 2 , '2_1' union all
select 6, 3 , '3_1' union all
select 7, 4 , '4_1'
select * from #tmp
--下面这句显然错误
select id,#tmp.Context,b.context as pContext from #tmp LEFT JOIN (select Context,pid from #tmp ) b ON b.pid=id
DROP TABLE #tmp
/*
id Context pContext
----------- ------------------------------ ------------------------------
1 1 1_1
2 2 2_1
3 3 3_1
4 4 4_1
5 1_1 NULL
6 2_1 NULL
6 3_1 NULL
7 4_1 NULL
*/