Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 加入2个cols id_Mysql_Sql - Fatal编程技术网

Mysql 加入2个cols id

Mysql 加入2个cols id,mysql,sql,Mysql,Sql,我有以下几张表: 表 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 |

我有以下几张表:

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语句。

您需要加入
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 

考虑到
表a
到列的
中不会有任何
NULL

您需要加入
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 
考虑到
表a
列的
中不会有任何
NULL

同意@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;
同意@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;