1

我想从三个表中选择一些值,这些表是通过外键关联的。

包含外键的表在该 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 |
+---------+-----------+----------+----------+

我必须在查询中更改什么才能获得该表?

4

1 回答 1

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;

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

于 2013-06-06T22:14:28.730 回答