简体   繁体   中英

left join doesn't return all rows Mysql

I have a table that has greenhouse information

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    
-------------------------------------------------------

I have this table that has the sowings of each of the greenhouses:

---------------------------------------------------------
  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
-------------------------------------------------------- 

This the table with the information of products:

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

I need to bring all the greenhouses regardless of whether you have products or not, but only bring me those that have products:

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

This is the result

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

But I need something like this:

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

I am using LEFT JOIN but it doesn't work, I hope that you can help me!

Your subquery would be much easier to follow with table aliases. You have the filtering conditions correct in the ON clause. However, you are aggregating by pr_grouper_details.id and this could be NULL .

Instead use the equivalent column s.id_grouper_detail . Then, I think you need to start with pr_grouper_details because you want to keep all of those rows. The ON s and WHERE clauses need to be adjusted:

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;

You have gd.id in your original query. That is a bad idea.

Try this one:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM