2016-12-04 41 views
0

嗯,我有如下表:加入2個的cols IDS

表A

from_no | to_no | msg  
43288519 | 59215348 | hi  
43288519 | 123456 | hello  
59215348 | 43288519 | how are you. 

表B

contactno | Name  
43288519 | Priyam  
123456 | ADC 
59215348 | Ankur 

結果我找的是:

from | to  | msg  
Priyam | Ankur | hi  
Priyam | adc | hello 
Ankur | Priyam | How are you 

請分享SQL語句相同。

回答

0

同意@Prdp。

另一種方式,假設contactno是獨一無二的,是:

select 
    (select name from tableb where contactono = t.from_no), 
    (select name from tableb where contactono = t.to_no), 
    msg 
from 
    tablea t; 
3

你需要加入TableB兩次

select B1.Name as from, B2.Name as to, a.Msg 
from TableA A 
join TableB B1 on A.from_no = B1.contactno 
join TableB B2 on A.to_no = B2.contactno 

考慮並且不會有在TableAfrom_no & to_no列的任何NULL值。