Mysql加入null数据

时间:2016-05-14 13:53:15

标签: php mysql join

我有3张表如下:

Table - Milestone
+----+----------+---------------------+---------------------+
| id | name     | details             | timestamp           |
+----+----------+---------------------+---------------------+
| 1  | 1st Year | One year Completed  | 2016-05-14 08:13:08 |
+----+----------+---------------------+---------------------+
| 2  | 2nd Year | Two years Completed | 2016-05-14 08:15:08 |
+----+----------+---------------------+---------------------+

Table Update-Type
+----+-----------+
| id | type      |
+----+-----------+
| 1  | Milestone |
+----+-----------+
| 2  | Offer     |
+----+-----------+

Table - Image
+----+-----------+-------------+-----------------+--------------------+
| id | update_id | update_type | path            | timestamp          |
+----+-----------+-------------+-----------------+--------------------+
| 1  | 1         | 1           | img/image_1.jpg | 2016-05-1408:13:08 |
+----+-----------+-------------+-----------------+--------------------+
| 2  | 1         | 1           | img/image_2.jpg | 2016-05-1408:14:08 |
+----+-----------+-------------+-----------------+--------------------+

在上面的表格中 - update_id = milestone_id或offer_id& update_type是更新的类型。

另外条件是,有些里程碑有图像,有些没有。我想加入3个表格,如果没有图像,则显示nullgroup_concat图像路径,其中包含类似的update_id和update_type。

我尝试过以下查询,但它只给了我里程碑图片。

SELECT milestone.id, milestone.name, milestone.details,
      GROUP_CONCAT(images.path) as path, update_type.id AS update_type
FROM milestone
    LEFT OUTER JOIN images ON milestone.id = images.update_id
    INNER JOIN update_type ON update_type.id = images.update_type
WHERE images.update_type ='1' AND update_type.type =  'Milestone'
GROUP BY milestone.id

我得到的当前输出:

+---+----------+--------------------+----------------------------------+-------------+
|   | name     | details            | path                             | update_type |
+---+----------+--------------------+----------------------------------+-------------+
| 1 | 1st Year | One year Completed | img/image_1.jpg, img/image_2.jpg | 1           |
+---+----------+--------------------+----------------------------------+-------------+

输出我应该得到:

+----+----------+---------------------+----------------------------------+-------------+
| id | name     | details             | path                             | update_type |
+----+----------+---------------------+----------------------------------+-------------+
| 1  | 1st Year | One year Completed  | img/image_1.jpg, img/image_2.jpg | 1           |
+----+----------+---------------------+----------------------------------+-------------+
| 2  | 2nd Year | Two years Completed | NULL                             | 1           |
+----+----------+---------------------+----------------------------------+-------------+

1 个答案:

答案 0 :(得分:1)

您可以查看here

LEFT JOIN images

您的错误在于使用WHERE images.update_type ='1'LEFT JOIN。如果您在左连接表的位置使用条件,则会消除LEFT JOININNER JOIN将充当WHERE - 不会返回空行。

第二种方法是修改查询中的WHERE (images.id IS NULL OR images.update_type ='1') 并允许图像为空值。像这样:

{{1}}