简体   繁体   English

从另一个表中选择数据SQL Select查询

[英]Selecting Data from another table SQL Select Query

Here is 2 Tables that are joined by the StaffID 这是由StaffID连接的2个表

Job Table
=========
JobID AssignedTo(StaffID) Created By(StaffID)
1     2                   1
2     3                   2

Staff Table
============
StaffID Name
1       May
2       Bob
3       Mary

I need An SQL Statement to get the job details with the corresponding staff name but have problems doing so as i'm unable to differentiate the columns as they are using the same table. 我需要一个SQL语句来获取具有相应员工姓名的工作详细信息,但是这样做有问题,因为我无法区分列,因为它们使用的是同一张表。 The end result should look like this 最终结果应如下所示

JobID Assigned To  Created By
1     Bob          May
2     Mary         Bob   

You need to join Staff table twice 您需要两次加入Staff

select J.JobId, S1.Name AS AssignedTo, S2.Name AS CreatedBy
from Job J
inner join Staff S1 on S1.StaffID = J.AssignedTo
inner join Staff S2 on S2.StaffID = J.CreatedBy

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

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