我如何在join中应用子查询

时间:2013-06-27 09:51:33

标签: sql

以下是我的表格:

userDetails表:

userid      name
 1          A   
 2          B

记录表:

Id          requesterId         ApproveId
1               1                   2  

我正在尝试以下查询:

select * from userDetils.name as Requester,**(What i can do here to get approver name)**
from Logs
join userDetail on Logs.requesterId = userDetails.userid

问题:

我如何获得请求者和批准者姓名

as

Requester   Approver
  A           B

2 个答案:

答案 0 :(得分:4)

select 
requester.name as Requester,
approver.name as Approver
from Logs as l
join userDetail as requester on l.requesterId = requester.userid
join userDetail as approver on l.ApproveId = approver.userid

答案 1 :(得分:0)

select requester.Name as Requester,
       approver.Name as Approver
from Logs log
join userDetail requester
on requester.userid = log.requesterId
join userDetail approver
on approver.userid = log.approverId

您可以使用不同的子句在同一个表上连接两次。