SQL如何正确地进行外连接

时间:2017-06-14 12:11:53

标签: mysql sql database select

我有一张桌子,里面装满了公司的电话号码。 我的问题是,我们的员工每个号码都有一排,所以如果他有一部主电话和一部手机,我们有两排他。

现在我需要显示所有员工的列表,包括他们的电话号码和手机号码(如果有的话)和电子邮件,但电子邮件在另一个表格中。 我正在使用SQL。

一个例子:

PhoneTable

   ID  | EmpID  |  FullName     |  Number        |  Type     |
----------------------------------------------------------------
   115 |    02  |  ManuelSan    | +34935556663   |   Fix     |
   116 |    02  |  ManuelSan    | +34652315453   |   Mobile  |
   117 |    06  |  Camillete    | +34934445621   |   Fix     |
   118 |    07  |  MarcusEsq    | +34932547841   |   Fix     |
   119 |    08  |  FionaYem     | +34965214785   |   Fix     |
   120 |    08  |  FionaYem     | +34652132124   |   Mobile  |

EmailTable

    ID   |   empID   |   Fullname   |        Email           |
-----------------------------------------------------------------
   25    |    02     |  ManuelSan   |  Manuelsan@gg.com      |
   26    |    06     |  Camillete   |  Camillete@gg.com      |
   27    |    07     |  MarcusEsq   |  MarcusEsq@gg.com      |
   28    |    08     |  FionaYem    |  FionaYem@gg.com       |

所以我想要这个输出

   Fullname |    Fix       |     Mobile    |      Email  
------------------------------------------------------------------
 ManuelSan  | +34935556663 | +34652315453  |  Manuelsan@gg.com
 Camillete  | +34934445621 |      NULL     |  Camillete@gg.com
 MarcusEsq  | +34932547841 |      NULL     |  MarcusEsq@gg.com
 FionaYem   | +34965214785 | +34652132124  |  FionaYem@gg.com

但我这样做:

SELECT distinct telf.Fullname, telf.Number, acti.EMAIL
  FROM PhoneTable telf
  left outer join EmailTable as acti on acti.empID = telf.empID

我知道我需要做别的事,但我不知道是什么。如果他有手机和手机,我每个员工会得两排。

我该怎么做?

此致

5 个答案:

答案 0 :(得分:5)

许多冗余数据,可能不一致;但是,如果我们假设empid定义了名称并且PhoneTable包含每个员工的条目,则查询可能如下所示。查询的第一部分将一个员工的不同记录合并为一个记录;然后左外连接获取相应的电子邮件。请注意,如果有员工拥有电子邮件但是没有一个电话号码,则查询仍然不完整:

select *
from (select empid,
          max(fullname),
          max(case when type='Fix' then Number else NULL end) as fix,
          max(case when type='Mobile' then Number else NULL end) as Mobile
      from PhoneTable
      group by empid) phone
  left outer join EMailTable e on phone.empid=e.empid

答案 1 :(得分:4)

试试这个:

select
e.FullName,
t1.Number as "Fix",
t2.Number as "Mobile",
em.Email
from
(
    select distinct
    EmpId,
    FullName
    from PhoneTable
) e
left join PhoneTable t1 on t1.EmpId = e.EmpId and t1.Type = 'Fix'
left join PhoneTable t2 on t2.EmpId = e.EmpId and t2.Type = 'Mobile'
left join EmailTable em on em.empID = e.EmpId

答案 2 :(得分:2)

我的方法是:

SELECT mail.FullName, phonefix.Number, phonemob.Number, mail.Email
  FROM EmailTable mail
FULL OUTER JOIN PhoneTable phonefix
 ON mail.empID = phonefix.empID
AND phonefix.Type = 'Fix'
FULL OUTER JOIN PhoneTable phonemob
 ON mail.empID = phonemob
AND phonemob.Type = 'Mobile'

当然,如果您有一个Employee表。从employee表开始,使用左外连接来链接到电话号码和邮件

答案 3 :(得分:1)

如果您有固定数量的手机类型,您可以通过每种类型的一个连接执行此操作,如下所示(假设您有一个员工表):

select e.EmpId, pt1.Number as "PtFix", pt2.Number as "PtMobile"
from Employee e
  left join PhoneTable pt1 
    on (e.EmpId = pt1.EmpId and pt1.Type = 'Fix')
  left join PhoneTable pt2 
    on (e.EmpId = PhoneTable.EmpId and pt2.Type = 'Mobile')

如果您有可能会引入更多手机类型(例如Office),您可以将它们聚合成一个数组或在mysql中连接:

select e.EmpId, group_concat(pt.Number) as numbers
from Employee e
 left join PhoneTable pt 
   on (e.EmpId = pt.EmpId)
group by e.EmpId

答案 4 :(得分:0)

您称之为电子邮件表的表是员工表。每个员工只有一个条目。因此每位员工只有一个电子邮件地址。至于电话号码,我认为每种类型和员工可以有零个或一个电话号码。 (如果可以有更多,我们只选一个。)

select e.fullname, p.fix, p.mobile, e.email
from emails
left join
(
  select 
    empid,
    max(case when type = 'Fix' then number end) as fix,
    max(case when type = 'Mobile' then number end) as mobile
  from phones
  group by empid
) p on p.empid = e.empid;