MySQL Group By functionality in different version

后端 未结 1 662
-上瘾入骨i
-上瘾入骨i 2020-12-19 22:45

Following is a simple SQL query:

SELECT * FROM *table_name*
GROUP BY *column_name*

In my system I have MySQL 5.5. It is working absolutely

相关标签:
1条回答
  • 2020-12-19 23:30

    First of all, please read Group by clause in mySQL and postgreSQL, why the error in postgreSQL?

    It is not SQL Standard behaviour.

    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.

    It looks like on second server you have acitivated ONLY_FULL_GROUP_BY mode.

    SELECT @@sql_mode;
    

    You could simulate this behaviour on your 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


    From 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.

    0 讨论(0)
提交回复
热议问题