在SQL查询中包含空值

时间:2013-07-08 20:29:33

标签: mysql

我的数据库中有以下四个表:

+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| complex_id   | int(11)     | NO   | PRI | NULL    | auto_increment |
| complex_name | varchar(45) | NO   |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+


+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| machine_id   | int(11)     | NO   | PRI | NULL    | auto_increment |
| complex_id   | int(11)     | NO   | MUL | NULL    |                |
| machine_name | varchar(45) | NO   |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+


+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| devices_id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| machine_id      | int(11)      | NO   | MUL | NULL    |                |
| description     | varchar(255) | NO   |     | NULL    |                |
| location        | varchar(255) | YES  |     | NULL    |                |
| verification    | varchar(255) | YES  |     | NULL    |                |
| rack_num        | varchar(8)   | YES  |     | NULL    |                |
| section_num     | varchar(8)   | YES  |     | NULL    |                |
| color_or_number | varchar(16)  | YES  |     | NULL    |                |
| normal_position | varchar(16)  | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+


+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| pnp_id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| devices_id | int(11)     | NO   | MUL | NULL    |                |
| pnp_num    | varchar(45) | NO   |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+

我试图将结果格式化为如下(NULL值显示为空白):

+------------+-------------+-----------------------+
| devices_id | description | pnpnum                |
+------------+-------------+-----------------------+
| 1          | ex          | 1234                  |
| 2          | ex2         | 2345                  |
| 3          | ex3         |                       |
| 4          | ex4         | 3456, 4567, 5678, 6879|
+------------+-------------+-----------------------+

使用以下SQL查询

SELECT *, GROUP_CONCAT(pnp.pnp_num separator ', ') pnpnum
FROM devices
JOIN pnp ON devices.devices_id = pnp.devices_id
WHERE devices.machine_id = 1
GROUP BY devices.devices_ID
ORDER BY devices.description;

我的结果相对接近,但是,如果设备有一个空的pnpnum,我就无法包含它。

+------------+-------------+-----------------------+
| devices_id | description | pnpnum                |
+------------+-------------+-----------------------+
| 1          | ex          | 1234                  |
| 2          | ex2         | 2345                  |
| 4          | ex4         | 3456, 4567, 5678, 6879|
+------------+-------------+-----------------------+

我的SQL语句中缺少的是什么,它允许我包含空值?

1 个答案:

答案 0 :(得分:3)

您需要使用LEFT JOIN,因为即使没有匹配项,它也会返回左表中的所有结果,并保留右表null中的字段。