现象:
执行sql SELECT COUNT(DISTINCT ( colum1 )),colum2, colum3 FROM table;报错ERROR 1140 (42000): In aggregated query without GROUP BY.....
原因:
mysql的sql_mode默认开启了only_full_group_by模式,导致mysql的sql的group by 规则跟Oracle类似了,去掉only_full_group_by模式就可以了
解决:
1.查看sql_mode
show variables like '%sql_mode'; (下面是执行结果)
show session variables like '%sql_mode%'; (下面是执行结果)
show global variables like '%sql_mode%'; (下面是执行结果)
能看到查询出来的value中确实有ONLY_FULL_GROUP_BY
2.修改sql_mode
set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
sql_model后面的值就是上面查询出来的值去掉ONLY_FULL_GROUP_BY
3.再次查询
show variables like '%sql_mode'; (下面是结果)
好了,现在已经去掉了ONLY_FULL_GROUP_BY,就不会报错了