日期:2014-05-17 浏览次数:20792 次
if OBJECT_ID('tempdb..#temp', 'u') is not null drop table #temp;
go
create table #temp( [ID] varchar(100), [USERID] varchar(100), [PARENTID] varchar(100), [USERNAME] varchar(100), [DEPT] varchar(100));
insert #temp
select 'GUID','Sunny01','John01','张三','AD' union all
select 'GUID','Max01','Sunny01','李四','AD' union all
select 'GUID','Peter01','Sunny01','王五','AD' union all
select 'GUID','Pony01','Sunny01','赵六','AD' union all
select 'GUID','Anni01','Sunny01','孙七','AD'
--SQL:
--查出所有有领导的人.如果领导的层级有多层,且不固定,请楼主给出具体需求.中间层的领导是否显示?
select * from #temp a
WHERE EXISTS
(
SELECT 1
FROM #temp b
WHERE a.PARENTID = b.USERID
)
/*
ID USERID PARENTID USERNAME DEPT
GUID Max01 Sunny01 李四 AD
GUID Peter01 Sunny01 王五 AD
GUID Pony01 Sunny01 赵六 AD
GUID Anni01 Sunny01 孙七 AD
*/