2013-06-27 54 views
0

在這裏,我可以申請子查詢在下面給出我的表:如何在加入

爲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 

問題:

我怎樣才能請求者和審批命名

Requester Approver 
    A   B 
+1

您必須加入兩次userDetails表,一次請求者和一次批准者。 – Sam

回答

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 
+0

謝謝@janne Matikainen – Sunny

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 

您可以在同一個表連接兩次,使用不同條款。

+0

感謝您的快速回復:-) – Sunny