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;