0

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

+--------------+-------------+------+-----+---------+----------------+
| 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 语句中缺少什么允许我包含空值?

4

1 回答 1

3

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

于 2013-07-08T20:33:07.060 回答