现在执行一个GROUP BY,其中SELECT列表里的NAME字段没有出现在GROUP BY 子句里
报错了,我们需要的是这种效果
mysql> select id,max(mydate) mydate,name from t group by id;
ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause
and contains nonaggregated column 'zhu.t.name' which is not functionally
dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
然后再看一下SQL_MODE 的参数设置,其中ONLY_FULL_GROUP_BY是默认ENABLED了
mysql> show variables like 'sql_mode'\G;
*************************** 1. row ***************************
Variable_name: sql_mode
Value: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,
NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,
NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
可是在MYSQL 5.7.5之前,默认ONLY_FULL_GROUP_BY是DISABLE的
我们现在将ONLY_FULL_GROUP_BY不开启试试,实际上如下并不是我要的效果
而且MYSQL不保证此时的NAME字段的值是正确的
mysql> set session sql_mode='';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select id,max(mydate) mydate,name from t group by id;
++++
| id | mydate | name |
++++
| 1 | 2016-10-28 | xx |
| 2 | 2016-10-30 | xx |
++++
2 rows in set (0.00 sec)
扫码关注