2016-12-04 47 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值。