Execute query SELECT @@sql_mode
to verify the ONLY_FULL_GROUP_BY
is inside the query result.
From MySQL documentation for ONLY_FULL_GROUP_BY
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.
It needs to either remove the above server setting to allow the query execution or in a more recommended way, add aggregate functions to columns not in your GROUP BY
, e.g. SELECT MAX(id)
, GROUP_CONCAT(id)
, etc.
Removing the ONLY_FULL_GROUP_BY
setting and running such non-standard SQL is permitted by MySQL but not recommended. Refer to documentation
If ONLY_FULL_GROUP_BY is disabled, a MySQL extension to the standard
SQL use of GROUP BY permits the select list, HAVING condition, or
ORDER BY list to refer to nonaggregated columns even if the columns
are not functionally dependent on GROUP BY columns. This causes MySQL
to accept the preceding query. In this case, the server is free to
choose any value from each group, so unless they are the same, the
values chosen are nondeterministic, which is probably not what you
want. Furthermore, the selection of values from each group cannot be
influenced by adding an ORDER BY clause. Result set sorting occurs
after values have been chosen, and ORDER BY does not affect which
value within each group the server chooses. Disabling
ONLY_FULL_GROUP_BY is useful primarily when you know that, due to some
property of the data, all values in each nonaggregated column not
named in the GROUP BY are the same for each group.
You can achieve the same effect without disabling ONLY_FULL_GROUP_BY
by using ANY_VALUE() to refer to the nonaggregated column.