MySQL Group By功能在不同的版本中
作者:互联网
以下是一个简单的SQL查询:
SELECT * FROM *table_name*
GROUP BY *column_name*
在我的系统中,我有MySQL 5.5.它工作得非常好.
而在我朋友的系统中,他有MySQL 5.7,并且他收到以下错误:
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY
clause and contains nonaggregated column ‘testdb.assetentry.entryId’
which is not functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
很明显,这种情况正在发生,因为版本不同.
但我想知道的是背后的原因.
任何人都可以解释一下.
解决方法:
首先,请阅读Group by clause in mySQL and postgreSQL, why the error in postgreSQL?
它不是SQL Standard行为.
07001
To disable the MySQL GROUP BY extension and enable standard SQL behavior, enable the ONLY_FULL_GROUP_BY SQL mode. In this case, columns not named in the GROUP BY clause cannot be used in the select list or HAVING clause unless enclosed in an aggregate function.
看起来在第二台服务器上你已经激活了ONLY_FULL_GROUP_BY模式.
SELECT @@sql_mode;
您可以在MySQL 5.5上模拟此行为:
SET SESSION sql_mode = 'ONLY_FULL_GROUP_BY';
SELECT *
FROM tab
GROUP BY col;
-- tab.col2' isn't in GROUP BY
从MySQL 5.7开始:
Implementation of the ONLY_FULL_GROUP_BY SQL mode has been made more
sophisticated, to no longer reject deterministic queries that
previously were rejected. In consequence, ONLY_FULL_GROUP_BY is now
enabled by default, to prohibit nondeterministic queries containing
expressions not guaranteed to be uniquely determined within a group.
标签:sql,mysql,aggregate-functions,mysql-error-1055 来源: https://codeday.me/bug/20190829/1758567.html