34,590
社区成员
发帖
与我相关
我的任务
分享
create table tab(index1 int,name varchar(16),superioridex int)
insert into tab select 1,'王',3 union all
select 3,'李',5 union all
select 5,'张',1
select b.name ,b.superioridex from tab a,tab b where a.index1=b.superioridex
index1 name superioridex
1 王 3
3 李 5
5 张 1
---lz是不要这结果啊-------
name superioridex
王 3
李 5
张 1
DECLARE @t TABLE([index] INT,name VARCHAR,superiorindex INT)
INSERT @t SELECT 1,'a',NULL
UNION ALL SELECT 2,'b',1
UNION ALL SELECT 3,'c',2
SELECT a.[index],a.name,b.name FROM @t a
LEFT JOIN @t b
ON a.superiorindex = b.[index]
/*
1 a NULL
2 b a
3 c b
*/
--这样。
select df_Name as 名称,
(select df_Name from udfdt_Department where df_Index=a.df_SuperiorDeptIndex) as 上级部门
from udfdt_Department a
SELECT *
FROM tba AS a
JOIN tba AS b
ON a.superiorindex=b.index