在FK中连接具有空值的表

时间:2013-06-06 22:11:05

标签: mysql join

我想从三个表中选择一些值,这些表与外键相关。

包含外键的表在该FK中可以具有空值。

当我从三个表中选择时,我没有得到FK为空的行。

例如,这些是我的表格:

表1(boninterne):

+---------+-----------+----------+----------+
|   num   |   date    | codePers | codeDept |
+---------+-----------+----------+----------+
|1        |12/10/2012 +  NULL    |  2       |
+---------+-----------+----------+----------+
|2        |12/10/2012 |  NULL    |  NULL    |
+---------+-----------+----------+----------+
|3        |12/10/2012 |  1       |  2       |
+---------+-----------+----------+----------+
|4        |12/10/2012 |  4       |  NULL    |
+---------+-----------+----------+----------+
|5        |12/10/2012 |  12      |  2       |
+---------+-----------+----------+----------+
|6        |12/10/2012 |  3       |  NULL    |
+---------+-----------+----------+----------+
|7        |12/10/2012 |  1       |  2       |
+---------+-----------+----------+----------+
|8        |12/10/2012 |  NULL    |  3       |
+---------+-----------+----------+----------+

表2(人员):

+---------+-----------+----------+
|codePers |prenomPers | nomPers  |
+---------+-----------+----------+
|1        |AA         +  ZZ      |
+---------+-----------+----------+
|4        |BB         |  WW      |
+---------+-----------+----------+
|3        |CC         |  XX      |
+---------+-----------+----------+
|12       |DD         |  VV      |
+---------+-----------+----------+

表3(部门):

+---------+-----------+
|codeDept |libDept    |
+---------+-----------+
|2        |DEPT A1    +
+---------+-----------+
|3        |DEPT B1    |
+---------+-----------+

这是我的查询:

SELECT num, date, CONCAT(prenomPers,' ',nomPers) AS nom, libDept 
FROM boninterne, personnel, departement
WHERE boninterne.codePers = personnel.codePers
AND boninterne.codeDept = departement.codeDept

我想要的表是:

+---------+-----------+----------+----------+
|   num   |   date    | nom      | libDept  |
+---------+-----------+----------+----------+
|1        |12/10/2012 +  NULL    |  DEPT A1 |
+---------+-----------+----------+----------+
|2        |12/10/2012 |  NULL    |  NULL    |
+---------+-----------+----------+----------+
|3        |12/10/2012 |  AA ZZ   |  DEPT A1 |
+---------+-----------+----------+----------+
|4        |12/10/2012 |  BB WW   |  NULL    |
+---------+-----------+----------+----------+
|5        |12/10/2012 |  DD VV   |  DEPT A1 |
+---------+-----------+----------+----------+
|6        |12/10/2012 |  CC XX   |  NULL    |
+---------+-----------+----------+----------+
|7        |12/10/2012 |  AA ZZ   |  DEPT A1 |
+---------+-----------+----------+----------+
|8        |12/10/2012 |  NULL    |  DEPT B1 |
+---------+-----------+----------+----------+

但我真正得到的是:

+---------+-----------+----------+----------+
|   num   |   date    | nom      | libDept  |
+---------+-----------+----------+----------+
|3        |12/10/2012 |  AA ZZ   |  DEPT A1 |
+---------+-----------+----------+----------+
|5        |12/10/2012 |  DD VV   |  DEPT A1 |
+---------+-----------+----------+----------+
|7        |12/10/2012 |  AA ZZ   |  DEPT A1 |
+---------+-----------+----------+----------+

我需要在查询中更改以获取该表吗?

1 个答案:

答案 0 :(得分:3)

您的结果是正确的,因为您正在使用内部联接。您可以使用正确的连接语法并使用left outer join来解决此问题:

SELECT num, date, CONCAT(prenomPers,' ',nomPers) AS nom, libDept 
FROM boninterne
left outer join personnel
    on boninterne.codePers = personnel.codePers
left outer join departement
     on boninterne.codeDept = departement.codeDept;

即使没有匹配项,这也会将所有内容保留在第一个表中。