Select 列表包含非聚合列

Select list contains nonaggregated column

更新后 MySQL 我注意到以下查询失败了

SELECT u.*, p.name as plan, COUNT(u.id) as totalprojects FROM users u LEFT JOIN plans p ON p.id = access LEFT JOIN maps m ON m.user_id = u.id WHERE u.email = 'john@doe.com'

In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'kontakt.u.id'; this is incompatible with sql_mode=only_full_group_by

有谁知道如何修复此查询以对错误进行排序?

您可以执行以下两项操作之一:

1) 更改您的查询,以便汇总 select 子句中的所有内容。像这样

SELECT u.email, p.name as plan, COUNT(u.id) as totalprojects FROM users u LEFT JOIN plans p ON p.id = access LEFT JOIN maps m ON m.user_id = u.id WHERE u.email = 'john@doe.com' group by u.email;

2) 更改 sql 模式以允许 mysql 到 运行 您的查询。像这样

[mysqld]
sql_mode = "NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

在您的 my.cnf 文件中。

请注意,这会设置各种 sql_mode 选项。您可以在这里阅读它们:http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html

在我的带有 Digital Ocean 的节点服务器上,我需要编辑的会议文件位于 /etc/mysql/mysql.conf.d/mysqld.cnf

在下面加入

[mysqld] sql_mode = "NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

然后 service mysql restart 解决了这个问题。

最简单的解决方案是 ANY_VALUE 函数: ANY_VALUE official documentation

SELECT name, ANY_VALUE(address), MAX(age) FROM t GROUP BY name;