2017-10-17 55 views
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看到,但我沒有理解,因爲它顯示了我不同的數據。

我希望你能幫幫我!

謝謝!

+0

GROUP BY無效。將在新版本的MySQL上引發錯誤(除非在兼容模式下),並且在舊版本中返回不可預測的數據。一般的GROUP BY規則說:「如果指定了GROUP BY子句,SELECT列表中的每個列引用必須標識一個分組列或作爲set函數的參數。」 – jarlh

+0

就像@jarlh所說的,你必須在選擇列表中使用所有聚合函數或組 –

+0

對不起,我是mysql新手,請你舉個例子吧! @jarlh –

回答

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

感謝您的幫助!

相關問題