#1055 - SELECT列表的表达式#6不在GROUP BY子句中,并且包含非聚合列

时间:2017-03-08 18:46:32

标签: mysql mysql-error-1055

我在MySQL 5.7中遇到了下面的查询问题,但是在MySQL 5.6中它运行良好。

每次都会显示此消息:

  

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

从mysql 5.7开始,您可以选择组中不存在的非聚合列 如果你想要以前版本的相同行为,你必须撤销sql_mode = only_full_group_by(使用SET sql_mode ='') 或者更准确地说,您应该按照未聚合的所有列来构建选择或分组 例如:

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