我有一个“reslookup”表,我想在其中选择向另一个reportingid报告的员工。
以下是我的表的简化版本,包含以下内容:

Emp_NO   Name    ReportingID
531       A         16
1572      B         531
1032      C         1572
and so..on..

Now, If I select ReportingID = 16 (in where condition), then Employees 531,1572,1032 should fetch,
Similarly, If I select ReportingID = 531 (in where condition), then Employees 1572,1032 should fetch,
Similarly, If I select ReportingID = 1572 (in where condition), then Employees 1032 should fetch
有什么解决办法吗??

最佳答案

下面的代码对您有帮助,

select Emp_No, Name, ReportingID
from Reslookup as t1
where ReportingID = 16
      or exists
       (   select *
           from Reslookup as t2
           where Emp_No = t1.ReportingID
           and
             (
                ReportingID = 16
                or exists
                 (
                    select *
                    from Reslookup as t3
                    where Emp_No = t2.ReportingID
                    and ReportingID = 16
                 )
              )
        )

你必须通过,这里我给你16分。所以这将返回三行。

关于mysql - MySQL从表中选择行,其中列链接到同一表中的另一列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48924436/

10-11 02:16