0

我在 MySQL 5.7 中对下面的查询有疑问,但在 MySQL 5.6 中它运行良好。

每次都会出现此消息:

1055 - SELECT 列表的表达式 #6 不在 GROUP BY 子句中,并且包含
在功能上不依赖于 GROUP BY 子句中的列的非聚合列“electricity_databases.electricity_invoices.date_inserted”;这与 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
4

2 回答 2

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
于 2017-03-08T18:49:49.930 回答
0

从 mysql 5.7 开始,如果您想要与以前版本相同的行为,您可以选择组中不存在的非聚合列,您必须撤消 sql_mode=only_full_group_by (使用 SET sql_mode = '')或更正确地,您应该构建一个 select 或 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
于 2017-03-08T18:54:45.137 回答