[英]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.