Expression #6 of SELECT list is not in GROUP BY 에러 해결방법 - MySQL 설정

쪼경·2022년 2월 3일
0

아 이번 프로젝트때 제일 많이 만난 에러 친구가 이친구 아닐까 싶다..

java.sql.SQLSyntaxErrorException: Expression #6 of SELECT list is not in GROUP BY 
clause and contains nonaggregated column 'xxxxxxxxxx' which is not functionally 
dependent on columns in GROUP BY clause; 
this is incompatible with sql_mode=only_full_group_by

MySQL에서 GROUP BY는 표준 SQL과 다르게 작동하기 때문에 헷갈리는 부분 중 하나이다.

GROUP BY 질의에서 SELECT할 수 있는 컬럼은 GROUP BY에 나열된 컬럼과 SUM(), COUNT() 같은 집계 함수(Aggregation Function)으로 한정된다.

내가 에러가 났던 이유는 GROUP BY에 사용되지 않은 객체를 SELECT하려고 했기 때문에 결과가 이상하게 출력된 것이다.

여러 해결 방법이 있었지만, MySQL 설정을 변경하는 방법을 선택하였다.




우선 터미널에서 mysql로 들어간다

MacBookAir ~ % mysql -u root -p
Enter password:



SELECT @@시스템변수 를 사용하여
@@SQL_MODE; 를 찾는다

mysql> SELECT @@SQL_MODE;
+-----------------------------------------------------------------------------------------------------------------------+
| @@SQL_MODE                                                                                                            |
+-----------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

여러 SQL_MODE가 찾아지는데, 그 중에서도 ONLY_FULL_GROUP_BY 를 지우면 정상 작동 한다.



MySQL 을 끌수 없으므로 set global 과 set session 을 이용하여 ONLY_FULL_GROUP_BY를 지운다.

mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)

mysql> set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)

set global 을 사용하면 중단 과정 없이 변경할 수 있다.
기본적인 설정은 /etc/my.cnf로 변경하여 재시작 하면 되지만 끌 수 없을때는 사용하면 유용하다.

set session 을 이용하면 특정 커넥션에 대해서 특정한 환경 변수로 결과를 얻을 수 있다.




코드 고칠 때 전문!!
나는 정말 정말 초보라서 하나하나 언제 누르고 언제 나가면 되는지 알려줬으면 좋겠는데 알려주는곳이 적어서 슬펐어서 전문을 놔둔다
하지만 하나씩 해보는게 본인 공부에는 제일 도움 된다!!!

MacBookAir ~ % mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 897
Server version: 8.0.27 Homebrew

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT @@SQL_MODE;
+-----------------------------------------------------------------------------------------------------------------------+
| @@SQL_MODE                                                                                                            |
+-----------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)

mysql> set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)

mysql> exit;






출처
http://jason-heo.github.io/mysql/2014/03/05/char13-mysql-group-by-usage.html
https://blog.naver.com/juner84/100124891560
https://diokun.wordpress.com/2013/12/11/mybatis%EC%97%90%EC%84%9C-mysql%EC%9D%98-set-session-variable%EC%9D%84-%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95/

profile
[개발자] Lv.1

0개의 댓글