[英]left join doesn't return all rows Mysql
我有一张有温室信息的表
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
但它不起作用,我希望你能帮助我!
使用表别名会更容易跟踪您的子查询。 您在ON
子句中设置了正确的过滤条件。 但是,您正在按pr_grouper_details.id
进行聚合,这可能是NULL
。
而是使用等效的列s.id_grouper_detail
。 然后,我认为您需要从pr_grouper_details
开始,因为您想保留所有这些行。 ON
和WHERE
子句需要调整:
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
这是一个坏主意。
试试这个:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.