简体   繁体   English

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

[英]sql query create new column and add data

在此输入图像描述

I want to retrieve the employee name with the name of their manager. 我想用他们经理的名字检索员工姓名。

For example: 例如:

Employee_Lastname: WARX Employee_Lastname:WARX
Employee_Firstname : CYNTHIA Employee_Firstname:CYNTHIA
MANAGER_NANE: SMITH MANAGER_NANE:史密斯

Warx Cythnia has tha manager with Manager_ID=7369 who is Smith John because Smith has the Employee_ID=7369 Warx Cythnia有经理,Manager_ID = 7369谁是Smith John,因为Smith有Employee_ID = 7369

I want to create the MANGER_NAME column and add data... 我想创建MANGER_NAME列并添加数据...

maybe you only want to project the value of the record and not by adding new column in your table right? 也许你只想投影记录的价值,而不是通过在表中添加新列? you only need to join this, 你只需加入这个,

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

or 要么

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

Normally, you wouldn't store such information. 通常,您不会存储此类信息。 But you can easily obtain it when querying: 但是查询时可以轻松获取:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM