使用另一个表中的userID获取firstname

时间:2014-06-20 09:46:27

标签: php mysql sql

我有以下mysql表: 表名:PaymentHitory

journeyID | passengerID  | driverID 
---------------------------  
1         |    1         |3  
2         |    2         |4 

表名:UserInformation

userID | firstName
------------------
1      |    ABC         
2      |    XYZ        
3      |    PQR         
4      |    MSN

我想要像

这样的结果
 journeyID | passengerName  | driverName 
------------------------------------------  
    1      |    ABC         |PQR  
    2      |    XYZ         |MSN 

1 个答案:

答案 0 :(得分:4)

这是表格的简单连接:

select journeyID, pax.firstName as passengerName, dri.firstName as driverName 
    from PaymentHistory pay 
    join UserInformation pax on pay.passengerID = pax.userID 
    join UserInformation dri on pay.passengerID = dri.userID