sql查询创建新列并添加数据

时间:2012-12-18 14:42:34

标签: sql sql-server-2008 tsql

enter image description here

我想用他们经理的名字检索员工姓名。

例如:

Employee_Lastname:WARX
Employee_Firstname:CYNTHIA
MANAGER_NANE:SMITH

Warx Cythnia有经理,经理人身份证号码= 7369谁是史密斯约翰,因为史密斯有员工_ID = 7369

我想创建MANGER_NAME列并添加数据......

2 个答案:

答案 0 :(得分:1)

也许您只想投影记录的值,而不是在表格中添加新列?你只需加入这个,

SELECT  a.*,
        b.Employee_LastName AS MANAGER_LASTNAME
FROM    EmpTable a
        LEFT JOIN EmpTable b
            ON a.Manager_ID = b.Employee_ID

SELECT  a.Employee_LastName,
        a.Employee_FirstName,
        b.Employee_LastName AS MANAGER_LASTNAME
FROM    EmpTable a
        LEFT JOIN EmpTable b
            ON a.Manager_ID = b.Employee_ID

答案 1 :(得分:1)

通常,您不会存储此类信息。但是你可以在查询时轻松获得它:

select
   t1.Employee_Lastname,
   t1.Employee_Firstname,
   t2.Employee_Lastname as manager_name
from [table] t1
       left join
     [table] t2
        on
           t1.manager_id = t2.employee_id