left join不会返回所有行Mysql

时间:2018-02-05 03:08:18

标签: mysql sql database left-join

我有一张有温室信息的表

pr_grouper_details

--------------------------------------------------------
  id  |  grouper_detail | status | periphery | id_tenant
--------------------------------------------------------
  1   |       1         |   100  |     0     |    1    
-------------------------------------------------------
  1   |       2         |   100  |     0     |    1    
-------------------------------------------------------
  1   |       3         |   100  |     0     |    1    
-------------------------------------------------------
  1   |       4         |   100  |     0     |    1    
-------------------------------------------------------

我有这张桌子,里面有每个温室的播种:

---------------------------------------------------------
  id  |  id_grouper_detail  | id_product  | type | status
---------------------------------------------------------
  1   |          1          |    1        |  SW  |  100
-------------------------------------------------------- 
  1   |          1          |    2        |  SW  |  100
-------------------------------------------------------- 
  1   |          2          |    1        |  SW  |  100
-------------------------------------------------------- 
  1   |          3          |    1        |  SW  |  100
-------------------------------------------------------- 

此表包含产品信息:

----------------------------
 id  |   product  | status |
----------------------------
  1  |   FLOWER1  |  100   |
---------------------------- 
  2  |   FLOWER2  |  100   |
---------------------------- 

无论你是否有产品,我都需要带上所有温室,但只带给我那些有产品的温室:

SELECT id, grouper_detail, GROUP_CONCAT(product SEPARATOR ' - ') AS products
FROM(
                    SELECT pr_grouper_details.id, pr_grouper_details.grouper_detail, pr_products.product
     FROM sw_sowing
                    INNER JOIN pr_products ON pr_products.id = sw_sowing.id_product
     LEFT JOIN pr_grouper_details ON pr_grouper_details.id = sw_sowing.id_grouper_detail
     AND pr_grouper_details.status = 100
     AND pr_grouper_details.periphery = 0
                    AND pr_grouper_details.id_tenant = 1
                    WHERE sw_sowing.type = 'SW'
                    AND sw_sowing.status = 100
                    GROUP BY pr_grouper_details.id, pr_products.id
) AS s
GROUP BY id

这是结果

----------------------------------------------
  id |   grouper_detail |  products 
----------------------------------------------
  1  |         1        |  FLOWER1 - FLOWER2  
----------------------------------------------
  2  |         2        |  FLOWER1  
----------------------------------------------
  3  |         3        |  FLOWER1  
----------------------------------------------

但我需要这样的事情:

----------------------------------------------
  id |   grouper_detail |  products 
----------------------------------------------
  1  |         1        |  FLOWER1 - FLOWER2  
----------------------------------------------
  2  |         2        |  FLOWER1  
----------------------------------------------
  3  |         3        |  FLOWER1  
----------------------------------------------
  4  |         4        |  NULL  
----------------------------------------------

我正在使用LEFT JOIN,但它不起作用,我希望你可以帮助我!

2 个答案:

答案 0 :(得分:0)

使用表别名可以更容易地遵循子查询。您在ON子句中具有正确的过滤条件。但是,您按pr_grouper_details.id汇总,可能是NULL

而是使用等效列s.id_grouper_detail。然后,我认为你需要从pr_grouper_details开始,因为你想保留所有这些行。需要调整ONWHERE条款:

SELECT s.id_grouper_detail, p.product
FROM pr_grouper_details gd LEFT JOIN
     sw_sowing s
     ON gd.id = s.id_grouper_detail AND
        s.type = 'SW' AND
        s.status = 100 LEFT JOIN
     pr_products p
     ON p.id = s.id_product
WHERE gd.status = 100 AND
      gd.periphery = 0 AND
      gd.id_tenant = 1
GROUP BY s.id_grouper_detail, p.product;

您的原始查询中包含gd.id。这是个坏主意。

答案 1 :(得分:0)

试试这个:

SELECT gd.grouper_detail, GROUP_CONCAT(product SEPARATOR ' - ') AS products
FROM pr_grouper_details gd LEFT JOIN
    sw_sowing s
ON gd.grouper_detail = s.id_grouper_detail AND
    s.type = 'SW' AND
    s.status = 100 LEFT JOIN
    pr_products p
ON p.id = s.id_product
WHERE gd.status = 100 AND
    gd.periphery = 0 AND
    gd.id_tenant = 1
GROUP BY s.id_grouper_detail