我有以下查询:
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
给我带来了以下结果:
------------------------------------------------
idProductionUnit | productionUnit | variety
------------------------------------------------
1 | 1 | YELLOW
------------------------------------------------
2 | 2 | YELLOW
------------------------------------------------
3 | 3 | YELLOW
------------------------------------------------
上面的结果很好,但每次运行查询时,品种列都会更改其值,即:
------------------------------------------------
idProductionUnit | productionUnit | variety
------------------------------------------------
1 | 1 | YELLOW
------------------------------------------------
2 | 2 | BLUE
------------------------------------------------
3 | 3 | YELLOW
------------------------------------------------
它也给了我以下结果:
------------------------------------------------
idProductionUnit | productionUnit | variety
------------------------------------------------
1 | 1 | BLUE
------------------------------------------------
2 | 2 | YELLOW
------------------------------------------------
3 | 3 | YELLOW
------------------------------------------------
我不知道是否必须由ORDER BY或GROUP BY看到它,但我还没有理解,因为它向我展示了不同的数据。
我希望你能帮助我!
谢谢!
答案 0 :(得分:0)
我可以解决我的问题,我改变了这样的查询:
SELECT pr_production_units_details.id AS idProductionUnit, pr_production_units_details.production_units_detail AS productionUnit, pr_varieties.variety
FROM (SELECT MAX(sw_sowing.id) AS id
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
)AS sw
INNER JOIN sw_sowing ON sw_sowing.id = sw.id
INNER JOIN pr_production_units_details ON pr_production_units_details.id = sw_sowing.id_production_unit_detail
INNER JOIN pr_varieties ON pr_varieties.id = sw_sowing.id_variety
WHERE sw_sowing.status != 0
AND sw_sowing.id_tenant =1
AND pr_production_units_details.id_grouper_detail = 13
GROUP BY pr_production_units_details.id, variety
我按照有关评论的说明进行操作,我的错误仅根据Mysql版本显示我
感谢您的帮助!