Sagar Kanabar
Sagar Kanabar

Reputation: 464

fetch firstname using userID from another table

I have following mysql table: Table Name:PaymentHitory

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

Table Name:UserInformation

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

I want result like

 journeyID | passengerName  | driverName 
------------------------------------------  
    1      |    ABC         |PQR  
    2      |    XYZ         |MSN 

Upvotes: 0

Views: 39

Answers (1)

RemyG
RemyG

Reputation: 496

It's a simple join on the tables:

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

Upvotes: 4

Related Questions