多行信息到一行

Several lines of information to a single line

我有关于客户的电子邮件地址和 phone 号码的信息,但数据存储在不同的 table 中,这导致每个客户有几行。

我有我的主要 table 和 customerId 等,我需要用电子邮件和 phone 号码加入。为此,我有一个 "translation" table 和一个连接 table 的 communication_id。

例如

主要table:

 CustomerID var1 var2 ...
 123        1    7

我需要使用翻译 table 将主要 table 与包含电子邮件和 phone 的 table 连接起来,如下所示

CustomerID CommID
123         780
123         781
123         782

并且带有电子邮件的 table 可能如下所示

commID email
780     a@a.com

和带有 phone 号码的 table 可能看起来像这样

commID phone
781     88888

如果我将上述 3 个 table 加入到我的主要 table 中,我所取得的成就是

CustomerID var1 var2    email    phone
123           1   7      a@a.com     ?
123           1   7       ?       88888
123           1   7       ?          ?

我明白为什么我得到 3 行,但我想要实现的是像这样的单行

CustomerID var1 var2 email    phone
123          1     7  a@a.com     88888

谢谢 编辑:

连接语法是

sel * from maintable 
left join Communication on maintable.CustomerID=Communication.CustomerID 
left join email on email.commID=Communication.CommID 
left join phone on phone.commID=Communication.CommID

要将多行合并为一行,您通常执行 GROUP BY:

SELECT maintable.CustomerID, MAX(email), MAX(phone)
FROM maintable
LEFT JOIN Communication ON maintable.CustomerID=Communication.CustomerID 
LEFT JOIN email ON email.commID=Communication.CommID 
LEFT JOIN phone ON phone.commID=Communication.CommID
GROUP BY maintable.CustomerID