GROUP BY 在 ONLY_FULL_GROUP_BY 模式下避坑1

1. 背景

在项目中发现有个人写了一条查询语句:

select sname,s_code,address from student group by (sname);

-- 表结构为:
mysql> desc student;
+-------------+--------------+------+-----+---------+-----------------------------+
| Field       | Type         | Null | Key | Default | Extra                       |
+-------------+--------------+------+-----+---------+-----------------------------+
| id          | int          | NO   | PRI | NULL    | auto_increment              |
| sname       | varchar(20)  | NO   | MUL | NULL    |                             |
| s_code      | int          | YES  |     | NULL    |                             |
| address     | varchar(100) | YES  |     | NULL    |                             |
| height      | double       | YES  | MUL | NULL    |                             |
| classid     | int          | YES  |     | NULL    |                             |
| create_time | datetime     | NO   |     | NULL    | on update CURRENT_TIMESTAMP |
+-------------+--------------+------+-----+---------+-----------------------------+

使用项目提供的环境运行发现,能够正确返回数据;

mysql> select sname,s_code,address from student group by (sname);
+-----------------+--------+---------+
| sname           | s_code | address |
+-----------------+--------+---------+
| 变成派大星      |      3 | 京东    |
| 学生1           |      1 | 上海    |
| 学生2           |      2 | 北京    |
| 学生4           |      4 | 联通    |
+-----------------+--------+---------+

但是连接自己搭建的数据,却报错。

ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'study.student.address' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

2. 问题排查

根据报错内容发现是 错误使用 group by 导致的,即不满足使用条件 SELECT子句中的列名必须为分组列或列函数。 但是为什么在项目中的mysql中能运行成功呢?是因为数据库模式设置不一样。

项目环境:

mysql> select @@sql_mode;
+-----------------------------------------------------------------------+
| @@sql_mode                                                            |
+-----------------------------------------------------------------------+
| STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO                        |
+-----------------------------------------------------------------------+

个人环境:

mysql> select @@sql_mode;
+-----------------------------------------------------------------------+
| @@sql_mode                                                            |
+-----------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO    |
+-----------------------------------------------------------------------+

相比于项目环境,多了个一个 ONLY_FULL_GROUP_BY 模式,即:

对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中。

3. 解决

方法1:修改项目中sql语句

select sname,s_code,address from student;

方法2:修改自己项目的模式为非 ONLY_FULL_GROUP_BY 模式

set sql_mode=(select replace(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
posted @ 2025-03-16 17:27  Myuniverse  阅读(91)  评论(0)    收藏  举报