4

我正在研究 MySQL 数据库。我有一张桌子empreporting,这张桌子包含 employeeidreportingemployeeid

SELECT e3.`ReportingEmployeeId` AS Level0,e2.`ReportingEmployeeId` AS Level1,e1.`ReportingEmployeeId` AS Level2,e1.`EmployeeId` AS Level3
FROM empreporting e1
LEFT JOIN empreporting e2 ON e1.`ReportingEmployeeId` = e2.`EmployeeId`
LEFT JOIN empreporting e3 ON e2.`ReportingEmployeeId` = e3.`EmployeeId`

上述查询给出以下结果:

0 级 1 级 2 级 3 级

\N \N 379 369

\N 379 484 372

\N \N \N 379

要求的结果格式是:

0 级 1 级 2 级 3 级

379 369 \N \N

379 484 372 \N

第379章     

请任何人帮助我。提前致谢

普拉卡什

4

2 回答 2

2

最终书面查询

SELECT 
CASE 
    WHEN e3.`ReportingEmployeeId` IS NOT NULL THEN e3.`ReportingEmployeeId`+1000000 
    WHEN e3.`ReportingEmployeeId` IS NULL AND e2.`ReportingEmployeeId` IS NOT NULL THEN e2.`ReportingEmployeeId`
    WHEN e3.`ReportingEmployeeId` IS NULL AND e2.`ReportingEmployeeId` IS NULL AND e1.`ReportingEmployeeId` IS NOT NULL THEN e1.`ReportingEmployeeId`
    WHEN e3.`ReportingEmployeeId` IS NULL AND e2.`ReportingEmployeeId` IS NULL AND e1.`ReportingEmployeeId` IS NULL AND e1.`EmployeeId`  IS NOT NULL THEN e1.`EmployeeId` 
END AS Level0,
CASE 
    WHEN e3.`ReportingEmployeeId` IS NOT NULL THEN e2.`ReportingEmployeeId`
    WHEN e3.`ReportingEmployeeId` IS NULL AND e2.`ReportingEmployeeId` IS NOT NULL THEN e1.`ReportingEmployeeId`
    WHEN e3.`ReportingEmployeeId` IS NULL AND e2.`ReportingEmployeeId` IS NULL AND e1.`ReportingEmployeeId` IS NOT NULL THEN e1.`EmployeeId`
    WHEN e3.`ReportingEmployeeId` IS NULL AND e2.`ReportingEmployeeId` IS NULL AND e1.`ReportingEmployeeId` IS NULL AND e1.`EmployeeId`  IS NOT NULL THEN NULL
END AS Level1,
CASE 
    WHEN e3.`ReportingEmployeeId` IS NOT NULL THEN e1.`ReportingEmployeeId`
    WHEN e3.`ReportingEmployeeId` IS NULL AND e2.`ReportingEmployeeId` IS NOT NULL THEN e1.`EmployeeId` 
    WHEN e3.`ReportingEmployeeId` IS NULL AND e2.`ReportingEmployeeId` IS NULL AND e1.`ReportingEmployeeId` IS NOT NULL THEN NULL
    WHEN e3.`ReportingEmployeeId` IS NULL AND e2.`ReportingEmployeeId` IS NULL AND e1.`ReportingEmployeeId` IS NULL AND e1.`EmployeeId`  IS NOT NULL THEN NULL
END AS Level2,
CASE
    WHEN e3.`ReportingEmployeeId` IS NOT NULL THEN e1.`EmployeeId` 
    WHEN e3.`ReportingEmployeeId` IS NULL AND e2.`ReportingEmployeeId` IS NOT NULL THEN NULL 
    WHEN e3.`ReportingEmployeeId` IS NULL AND e2.`ReportingEmployeeId` IS NULL AND e1.`ReportingEmployeeId` IS NOT NULL THEN NULL
    WHEN e3.`ReportingEmployeeId` IS NULL AND e2.`ReportingEmployeeId` IS NULL AND e1.`ReportingEmployeeId` IS NULL AND e1.`EmployeeId`  IS NOT NULL THEN NULL
END AS  Level3
FROM empreporting e1
LEFT JOIN empreporting e2 ON e1.`ReportingEmployeeId` = e2.`EmployeeId` 
LEFT JOIN empreporting e3 ON e2.`ReportingEmployeeId` = e3.`EmployeeId` 
于 2013-09-19T11:08:53.993 回答
0

尝试

SELECT e1.EmployeeId AS Level0,e2.`EmployeeId` AS Level1,e3.`EmployeeId` AS Level2,e3.`ReportingEmployeeId` AS Level3
FROM empreporting e1
LEFT JOIN empreporting e2 ON e1.`ReportingEmployeeId` = e2.`EmployeeId`
LEFT JOIN empreporting e3 ON e2.`ReportingEmployeeId` = e3.`EmployeeId`
于 2013-09-19T08:03:14.590 回答