2017-03-08 33 views
-1

我在MySQL 5.7中遇到了以下查詢問題,但在MySQL 5.6中運行良好。#1055 - SELECT列表中的表達式#6不在GROUP BY子句中並且包含非聚集列

此消息每次出現:

1055 - 表達#SELECT列表的6不在GROUP BY子句中包含非聚合列
「electricity_databases.electricity_invoices.date_inserted」,這並不功能依賴於列在GROUP BY子句中;這是用的sql_mode = only_full_group_by

SQL代碼不兼容:

SELECT 
homes.id, 
homes.homeName, 
homes.city, 
homes.date_registered, 
ROUND(SUM(electricity_invoices.total), 2) AS TotalPrice, 
DATEDIFF(NOW(), electricity_invoices.date_inserted) AS last_insert_in_days, 
MAX(electricity_invoices.date_inserted) AS last_insert, 
COUNT(electricity_invoices.homeID) AS countPaymentTimes, 
MAX(electricity_invoices.currRead) AS currRead, 
MAX(electricity_invoices.prevRead) AS prevRead, 
ROUND(MAX(electricity_invoices.currRead) - MAX(electricity_invoices.prevRead), 1) AS lastComp, 
customer.name 

FROM homes 

LEFT JOIN electricity_invoices ON 
homes.id = electricity_invoices.homeID 

LEFT JOIN customer ON 
homes.id = customer.homeID 

GROUP BY homes.id 
ORDER BY homes.id 

回答

0

原因是在MySQL的最新版本,是默認情況下是不允許出現在group by子句中添加非聚集列。您可以通過從完整的組模式中禁用sql_mode來禁用此行爲。

只需在group by子句中添加非聚合列即可。

select homes.id, 
    homes.homeName, 
    homes.city, 
    homes.date_registered, 
    ROUND(SUM(electricity_invoices.total), 2) as TotalPrice, 
    DATEDIFF(NOW(), electricity_invoices.date_inserted) as last_insert_in_days, 
    MAX(electricity_invoices.date_inserted) as last_insert, 
    COUNT(electricity_invoices.homeID) as countPaymentTimes, 
    MAX(electricity_invoices.currRead) as currRead, 
    MAX(electricity_invoices.prevRead) as prevRead, 
    ROUND(MAX(electricity_invoices.currRead) - MAX(electricity_invoices.prevRead), 1) as lastComp, 
    customer.name 
from homes 
left join electricity_invoices on homes.id = electricity_invoices.homeID 
left join customer on homes.id = customer.homeID 
group by homes.id, 
    homes.homeName, 
    homes.city, 
    homes.date_registered, 
    customer.name 
+0

我該怎麼辦,你能解釋一下我請 – mohdadawe

+0

@mohdadawe - 看看http://stackoverflow.com/questions/23921117/disable-only-full-group-by – GurV

0

開始到mysql 5.7你可以選擇在組 不存在非聚集列,如果你願意,你必須撤銷的sql_mode = only_full_group_by以前的版本相同的行爲(使用SET的sql_mode =「」) 或者更恰當你應該BUIL一個選擇或不聚集 中所有列assing到組例如:

SELECT 
homes.id, 
homes.homeName, 
homes.city, 
homes.date_registered, 
ROUND(SUM(electricity_invoices.total), 2) AS TotalPrice, 
DATEDIFF(NOW(), electricity_invoices.date_inserted) AS last_insert_in_days, 
MAX(electricity_invoices.date_inserted) AS last_insert, 
COUNT(electricity_invoices.homeID) AS countPaymentTimes, 
MAX(electricity_invoices.currRead) AS currRead, 
MAX(electricity_invoices.prevRead) AS prevRead, 
ROUND(MAX(electricity_invoices.currRead) - MAX(electricity_invoices.prevRead), 1) AS lastComp, 
customer.name 

FROM homes 

LEFT JOIN electricity_invoices ON 
homes.id = electricity_invoices.homeID 

LEFT JOIN customer ON 
homes.id = customer.homeID 

GROUP BY homes.id, homes.homeName,homes.city, homes.date_registered, DATEDIFF(NOW(), electricity_invoices.date_inserted) AS last_insert_in_days 
ORDER BY homes.id 

,或者由於事實上,你並不需要特定的值這些列,用(假)這些列聚集

SELECT 
homes.id, 
min(homes.homeName), 
min(homes.city), 
min(homes.date_registered), 
ROUND(SUM(electricity_invoices.total), 2) AS TotalPrice, 
min(DATEDIFF(NOW(), electricity_invoices.date_inserted) AS last_insert_in_days), 
MAX(electricity_invoices.date_inserted) AS last_insert, 
COUNT(electricity_invoices.homeID) AS countPaymentTimes, 
MAX(electricity_invoices.currRead) AS currRead, 
MAX(electricity_invoices.prevRead) AS prevRead, 
ROUND(MAX(electricity_invoices.currRead) - MAX(electricity_invoices.prevRead), 1) AS lastComp, 
customer.name 

FROM homes 

LEFT JOIN electricity_invoices ON 
homes.id = electricity_invoices.homeID 

LEFT JOIN customer ON 
homes.id = customer.homeID 

GROUP BY homes.id 
ORDER BY homes.id 
+0

所有這些解決方案都沒有工作請幫忙 – mohdadawe

+0

你有錯誤? – scaisEdge

+0

如何禁用sql_mode,如果我禁用它可以損壞數據庫 – mohdadawe

相關問題