SQL请求外键在同一个表上

时间:2015-01-16 00:28:15

标签: sql select foreign-keys primary-key

我有一个表T,包含以下列 - ID_Employee(主键) - NAME_Employee - ID_Manager(同一个表上的外键)

为了显示表格的行,我这样做: SELECT ID_Employee,NAME_Employee,ID_Manager FROM T

但是如何显示经理的NAME而不是他的ID? 我是SQL的初学者:)

由于

1 个答案:

答案 0 :(得分:2)

欢迎使用Stack Overflow!

试试这个:

SELECT a.ID_Employee, 
  a.NAME_Employee, 
  b.ID_Employee AS ManagerID, 
  b.Name_Employee AS Manager
FROM T a
INNER JOIN T b
ON a.ID_Manager = b.ID_Employee