MySQL将值从一列匹配到另一列并返回一个值

时间:2017-08-11 16:46:53

标签: mysql

Username    Employee_Name   Is_Employed L4_Username
aaa123      A1              Y           aaa131
aaa124      A2              Y           aaa131
aaa125      A3              Y           aaa131
aaa126      A4              Y           aaa131
aaa127      A5              Y           aaa131
aaa128      A6              Y           aaa131
aaa129      A7              Y           aaa131
aaa130      A8              Y           aaa130
aaa131      A9              Y           aaa130

我需要对此数据执行的操作是尝试并创建一个可以找到aaa131(例如)的语句并返回该L4_Username的员工名称并将其显示为AS L4_Employeename,以便我得到以下内容:

Username    Employee_Name   Is_Employed L4_Username L4_Employeename
aaa123      A1              Y           aaa131      A9
aaa124      A2              Y           aaa131      A9
aaa125      A3              Y           aaa131      A9
aaa126      A4              Y           aaa131      A9
aaa127      A5              Y           aaa131      A9
aaa128      A6              Y           aaa131      A9
aaa129      A7              Y           aaa131      A9
aaa130      A8              Y           aaa130      A8
aaa131      A9              Y           aaa130      A8

它基本上需要像索引匹配一样并返回employee_name AS L4_EmployeeName

有什么想法吗?

这是我开始的地方

SELECT *
FROM Employee_Team a
    INNER JOIN Workforce_Detail b ON a.Username = b.Username
WHERE Is_Employed = 'Y'

编辑:根据评论,我不清楚哪些表格有什么:

  • 用户名在两个表格中。
  • L4_Username仅适用于Workforce_Detail
  • Employee_Name仅在Workforce_Detail

0 个答案:

没有答案