[MySQL] ์ค๋ฅ 1055 : ONLY_FULL_GROUP_BY : GROUP BY ํด๊ฒฐ ๋ฐฉ๋ฒ
MySQL์์ GROUP BY ๋ฅผ ์ฌ์ฉํ๋ค๋ณด๋ฉด ์ข ์ข ๋ค์๊ณผ ๊ฐ์ ์๋ฌ์ ๋ง์ฃผํ ์ ์๋ค
โ
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and
contains nonaggregated column 'DATABASE.TABLE.COLUMN' which is not functionally dependent on
columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
๋ถ๋ช ํ ๋ฌธ๋ฒ์ ํ๋ฆฌ์ง ์์ ๊ฒ ๊ฐ์๋ฐ ์ด๊ฒ ๋ฌด์จ ์๋ฆฌ๋ ๋ง์ธ๊ฐ? ์ถ๋ค๋ฉด ๋ค์์ ํ์ธํด๋ณด๋ฉด ๋๋คโ
https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
MySQL :: MySQL 5.7 Reference Manual :: 12.20.3 MySQL Handling of GROUP BY
12.20.3 MySQL Handling of GROUP BY SQL-92 and earlier does not permit queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are not named in the GROUP BY clause. For example, this query is illegal in stan
dev.mysql.com
ํด๋น ๋ฌธ์์ ๋ฐ๋ฅด๋ฉด ๋ก์ปฌ MySQL์ 5.7.14๋ก ์ ๊ทธ๋ ์ด๋ํ๋ฉด์
group by ์ฟผ๋ฆฌ์ ๋ํ ๋นํจ์จ์ฑ ํด๊ฒฐ์ ์ํด sql_mode ํ๋๊ทธ์ only_full_group_by ๋ฅผ ์ถ๊ฐ์์ผฐ๋ค๊ณ ํ๋ค
โ
๊น๊ฒ ์ดํดํ ํ์ ์์ด ๋ฐ๋ก ํด๊ฒฐํ์
mysql> SHOW VARIABLES LIKE 'SQL_MODE';
+---------------+---------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------+---------------------------------------------------------------------------------------------+
| sql_mode | ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+---------------+---------------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> select @@sql_mode;
+---------------------------------------------------------------------------------------------+
| @@sql_mode |
+---------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,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 ํ๋๊ทธ๋ฅผ ์ง์์ค๋ค
โ
๊ธฐ์กด์ ๊ฐ๋ค์ ๋ณต์ฌํ ํ ONLY_FULL_GROUP_BY ๋ง ์ ์ธํ๊ณ ๋ค์ ์ธํ ํด์ฃผ๋ฉด ๋๋ค
SET @@SQL_MODE='NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected, 1 warning (0.00 sec)
๋ค์ sql_mode๋ฅผ ์กฐํํด๋ณด๋ฉด ONLY_FULL_GROUP_BY ๊ฐ ์ฌ๋ผ์ ธ ์์์ ํ์ธํ ์ ์๋ค
group by ๋ฅผ ์ฌ์ฉํ์ ๋ ๊ฒฐ๊ณผ๊ฐ ์ ๋๋ก ์ถ๋ ฅ๋๋ค๋ฉด ์ฑ๊ณต!