I have upgraded my system and have installed MySql 5.7.9 with php for a web application I am working on. I have a query that is dynamically created, and when run in older ve
Use ANY_VALUE()
to refer to the nonaggregated column.
SELECT name, address , MAX(age) FROM t GROUP BY name; -- fails
SELECT name, ANY_VALUE(address), MAX(age) FROM t GROUP BY name; -- works
From MySQL 5.7 docs:
You can achieve the same effect without disabling
ONLY_FULL_GROUP_BY
by usingANY_VALUE()
to refer to the nonaggregated column....
This query might be invalid with
ONLY_FULL_GROUP_BY
enabled because the nonaggregated address column in the select list is not named in theGROUP BY
clause:SELECT name, address, MAX(age) FROM t GROUP BY name;
...
If you know that, for a given data set, each name value in fact uniquely determines the address value, address is effectively functionally dependent on name. To tell MySQL to accept the query, you can use the
ANY_VALUE()
function:SELECT name, ANY_VALUE(address), MAX(age) FROM t GROUP BY name;