Select list contains nonaggregated column

user6885115 picture user6885115 · Oct 19, 2016 · Viewed 8k times · Source

Since updating MySQL I've noticed the following query fails

SELECT u.*, p.name as plan, COUNT(u.id) as totalprojects FROM users u LEFT JOIN plans p ON p.id = access LEFT JOIN maps m ON m.user_id = u.id WHERE u.email = '[email protected]'

In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'kontakt.u.id'; this is incompatible with sql_mode=only_full_group_by

Does anyone know how to get this query fixed to sort the error?

Answer

Shadi Akil picture Shadi Akil · Jan 4, 2019

the easiest solution is ANY_VALUE function: ANY_VALUE official documentation

SELECT name, ANY_VALUE(address), MAX(age) FROM t GROUP BY name;