我想从三个表中选择一些值,这些表是通过外键关联的。
包含外键的表在该 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 |
+---------+-----------+----------+----------+
我必须在查询中更改什么才能获得该表?