MySQL 不同版本中的分组功能
MySQL Group By functionality in different version
以下是一个简单的 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
很明显,这是因为版本不同。
但我想知道的是这背后的原因。
谁能解释一下。
首先请阅读
这不是 SQL 标准行为。
12.16.3 MySQL Handling of GROUP BY
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 查询:
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
很明显,这是因为版本不同。
但我想知道的是这背后的原因。
谁能解释一下。
首先请阅读
这不是 SQL 标准行为。
12.16.3 MySQL Handling of GROUP BY
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.