为什么缺一行?

时间:2013-09-07 18:42:25

标签: sql hierarchy

我有一个小的层次表。

        NR  ENAME       BOSS 
       | 1| Johnson   | NULL|
       | 2| Jackson   | 1   |
       | 3| Wright    | 1   |
       | 4| Carell    | 3   |
       | 5| Statham   | 3   |
       | 6| Lincoln   | 4   |

现在我想看看每个员工和他们的老板。

SELECT Employee.ENAME Employee, BOSS.ENAME Boss
FROM Employee
JOIN Employee AS Boss ON Employee.BOSS=Boss.NR

通过运行此操作,我得到除 Johnson 之外的所有员工。 如何使用Employee Johnson和Boss NULL获取一行呢?

1 个答案:

答案 0 :(得分:1)

你正在桌面上SELF JOIN,你的BOSS栏对Johnson有NULL的价值。你需要这样做: -

SELECT Employee.ENAME Employee, BOSS.ENAME Boss
FROM Employee
LEFT OUTER JOIN Employee AS Boss ON Employee.BOSS=Boss.NR