[DEV SQL] 7. [MySQL, MariaDB] GROUP BY- this is incompatible with sql_mode=only_full_group_by


SELECT list is not in GROUP BY clause and contains nonaggregated column ‘컬럼명’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

SELECT 목록에 GROUP BY 절에 없으며 집계되지 않은 컬럼 ‘컬럼 명’을 포함되서 발생하는 에러이다.



only_full_group_by

mysql 5.7.5버전부터 sql_mode로 추가된 설정으로
SELECT 목록에 GROUP BY 절에 없으며 집계되지 않은 열 ‘컬럼 명’을 포함될 때 exception 발생 여부를 결정하는 옵션이다.

결론적으로 ANY_VALUE()를 사용해서 GROUP BY 절에 없는 컬럼도 SELECT 절에서 사용 할 수 있다.


ANY_VALUE() 집계함수를 사용

오류 발생

SELECT
    NAME
    , AGE
    , GRP
FROM
    t
GROUP BY NAME


정상 실행

SELECT
    NAME
    , ANY_VALUE( AGE ) AS AGE
    , ANY_VALUE( GRP ) AS GRP
FROM
    t
GROUP BY NAME


sql_mode 확인

select @@sql_mode;
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER

‘ONLY_FULL_GROUP_BY’ 가 존재하면 only_full_group_by 옵션이 설정되어 있는 상태이다.




[참고]

  • https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html