I have the following query:
SELECT pr_production_units_details.id AS idProductionUnit, pr_production_units_details.production_units_detail AS productionUnit,
IF(pr_varieties.variety IS NULL, 'SIN SEMBRAR O ERR', pr_varieties.variety) AS variety
FROM pr_production_units_details
LEFT JOIN (
SELECT MAX(sw_sowing.id) AS ids, sw_sowing.id_production_unit_detail, sw_sowing.id_variety
FROM sw_sowing
WHERE sw_sowing.status != 0
AND sw_sowing.id_tenant = 1
AND YEARWEEK(sw_sowing.date) <= 201741
GROUP BY sw_sowing.id_production_unit_detail, id_variety
ORDER BY ids DESC
) AS sw ON pr_production_units_details.id = sw.id_production_unit_detail
INNER JOIN pr_varieties ON sw.id_variety = pr_varieties.id
WHERE pr_production_units_details.id_grouper_detail = 1
AND pr_production_units_details.status = 100
AND pr_production_units_details.id_tenant = 1
GROUP BY pr_production_units_details.id
that brings me the following result:
------------------------------------------------
idProductionUnit | productionUnit | variety
------------------------------------------------
1 | 1 | YELLOW
------------------------------------------------
2 | 2 | YELLOW
------------------------------------------------
3 | 3 | YELLOW
------------------------------------------------
The previous result is fine, but every time I run the column variety changes its values, that is,
------------------------------------------------
idProductionUnit | productionUnit | variety
------------------------------------------------
1 | 1 | YELLOW
------------------------------------------------
2 | 2 | BLUE
------------------------------------------------
3 | 3 | YELLOW
------------------------------------------------
or you can also leave like this;
------------------------------------------------
idProductionUnit | productionUnit | variety
------------------------------------------------
1 | 1 | BLUE
------------------------------------------------
2 | 2 | YELLOW
------------------------------------------------
3 | 3 | YELLOW
------------------------------------------------
I do not know if I have to see the ORDER BY or the GROUP BY but I have not understood because it shows me different data.