主管表上的SQL查询

时间:2017-05-21 16:46:12

标签: sql sql-server

SQL Server中用于下面给出的标记查询的SQL查询是什么?

查找Bob的所有主管(直接和间接) enter image description here

4 个答案:

答案 0 :(得分:0)

;WITH CTE AS (
SELECT Supervisor
FROM t
WHERE Person='Bob'
UNION ALL
SELECT tt.Supervisor
FROM t tt
INNER JOIN CTE ON CTE.Supervisor = tt.Person
)
SELECT *
FROM CTE

Test

答案 1 :(得分:0)

试试这个:

DECLARE @T TABLE (Person NVARCHAR(30) , Supervisor NVARCHAR(30));
INSERT INTO @T VALUES
('Bob','Alice'),('Mary','Susan'),('Alice','David'),('David','Mary');

select T.Supervisor
from @T T
Where T.Person = 'Bob'
union all
Select TT.Supervisor
From @T TT inner join @T A
on  A.Supervisor = TT.Person

答案 2 :(得分:0)

1 =选择主管      来自emp-super      其中person ='bob';

2 =选择主管       来自emp-super       人=       (选择主管          来自emp-super
         其中person ='bob');

答案 3 :(得分:0)

select T.supervisor, F.supervisor 
from emp_Super T, emp_Super F 
where T.person = 'Bob' 
  and T.supervisor = F.person;