Fix MySQL Error : GROUP BY incompatible with sql_mode=only_full_group_by in OSX

15 December 2016 on MySQL, OSX, and Fix. 1 minute

When you upgrade your MySQL server version to 5.7.5 or later and any of your queries include GROUP BY clause. Then you will get an error cased by the update to MySQL 5.7.5 or later as below

Error Number: 1055

Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘columnname.id’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

This is due to MySQL 5.7.5+ changed the way GROUP BY behaved in order to be SQL99 compliant . This may affect some queries.

The ONLY_FULL_GROUP_BY mode

According to MySQL Documentation ONLY_FULL_GROUP_BY mode

Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns.

This not only means that you cannot mess up your GROUP BY-queries anymore as MySQL will now reject an improper GROUP BY query, it will also not require you to write non-sensical “dummy” aggregates over expressions that can only have one value per aggregated result row.

The workaround

If you are running MySQL 5.7.7+ , Edit your my.cnf file and remove the ONLY_FULL_GROUP_BY option from sql_mode.

If you used Homebrew to install/upgrade Mysql, then you can fix the issue by copying the default my.cnf file located in MySQL project path to /etc/my.cnf

sudo cp $(brew --prefix mysql)/support-files/my-default.cnf /etc/my.cnf

and then Change sql_mode in /etc/my.cnf to this

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

Then Restart the MySQL