简体   繁体   中英

Selecting Data from another table SQL Select Query

Here is 2 Tables that are joined by the StaffID

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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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