0
運行時間
我有以下查詢:查詢顯示不同的數據每個我在MySQL
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看到,但我沒有理解,因爲它顯示了我不同的數據。
我希望你能幫幫我!
謝謝!
GROUP BY無效。將在新版本的MySQL上引發錯誤(除非在兼容模式下),並且在舊版本中返回不可預測的數據。一般的GROUP BY規則說:「如果指定了GROUP BY子句,SELECT列表中的每個列引用必須標識一個分組列或作爲set函數的參數。」 – jarlh
就像@jarlh所說的,你必須在選擇列表中使用所有聚合函數或組 –
對不起,我是mysql新手,請你舉個例子吧! @jarlh –