Error related to only_full_group_by when executing a query in MySql

前端 未结 18 2450
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 04:39

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

18条回答
  •  伪装坚强ぢ
    2020-11-21 04:51

    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 using ANY_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 the GROUP 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;
    

提交回复
热议问题