2016-05-03 193 views
2

下面的MySQL集團通過功能是一個簡單的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

可以清楚地看到這種情況發生,因爲版本不同。

但我想知道的是背後的原因。

任何人都可以解釋。

回答

4

首先,閱讀Group by clause in mySQL and postgreSQL, why the error in postgreSQL?

它不是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 

SqlFiddleDemo


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.

+1

ONLY_FULL_GROUP_BY默認啓用5.7(這是一件好事,但會打破很多舊的MySQL應用程序) –

+0

@MichaelBerkowski謝謝,我加了這個回答 – lad2025