GROUP BY 쿼리 에러 해결 방법
GROUP BY 쿼리 에러
Expression of 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
원인
http://jason-heo.github.io/mysql/2014/03/05/char13-mysql-group-by-usage.html
GROUP BY를 사용하는 경우, SELECT할 수 있는 컬럼은 GROUP BY에 나열된 컬럼과 SUM(), COUNT() 같은 집계 함수(Aggregation Function)으로 한정
sql 표준 문법은 group by 사용시 select 의 칼럼중 집계함수에 쓰이는 것을 제외한 모든 칼럼을 기입해야 한다.
3. 해결방법 1.
쿼리 변경, group by 에 모두 기입
select col1, col2, count(col3) from table1 group by col1, col2; # 집계함수 이외의 모든 select 칼럼을 기입
3. 해결방법 2.
쿼리 변경 , 비 집계 칼럼에 ANY_VALUE() 함수 사용
select col1, ANY_VALUE(col2), count(col3) from table1 group by col1;
3. 해결방법 3.
mysql 설정 변경
sql_mode 에서 ONLY_FULL_GROUP_BY 설정을 뺀다.
SET SESSION sql_mode = 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES';
- 설정파일(my.cnf)에서 ONLY_FULL_GROUP_BY 를 찾아 삭제한다.