2013-11-09 47 views
2

我在表A中有兩個表兩列,使用表B作爲參考。我試圖使用內部連接,但我只得到一列結果。例如, 。Mysql:如何從同一個表中加入2列並使用另一個表作爲參考

Table A 
Reciever | Sender 
     1 | 2 
     1 | 3 

Table B 
id | Name | Lastname 
1 | James | Todd 
2 | Jimmy | Jones 
3 | Tom | Brown 

Desired result from joining the tables would be 

Table A 
Reciever | Sender 
James Todd | Jimmy Jones 
James Todd | Tom Brown 

回答

2
SELECT 
    CONCAT(t1.Name, ' ', t1.Lastname) As Reciever, 
    CONCAT(t2.Name, ' ', t2.Lastname) As Sender 
FROM 
    TableA INNER JOIN TableB t1 ON TableA.Reciever=t1.id 
    INNER JOIN TableB t2 ON TableA.Sender=t2.id 

請參閱小提琴here

相關問題