mysql 5.7 only_full_group_by (mac)
发布时间:2018-04-25作者:laosun阅读(2113)
Expression #10 of SELECT list is not in GROUP BY clause and contains nonaggregated ... dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by。
错误提示如下:Expression #10 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'd_sunjs.d.label' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
认真看错误的提示发现是在group by中的字段比较selelct中的字段差了一个month_id, 而正好是month_id报错。this is incompatible with sql_mode=only_full_group_by这句话提示了这违背了mysql的规则,only fully group by,也就是说在执行的时候先分组,根据查询的字段(select的字段)在分组的内容中取出,所以查询的字段全部都应该在group by分组条件内;一种情况例外,查询字段中如果含有聚合函数的字段不用包含在group by中,就像我上面的MAX(total_fee),至于为什么,我也不抬明白。
后来发现Order by排序条件的字段也必须要在group by内,排序的字段也是从分组的字段中取出。 不明白的可以去看一下。
解决办法:select字段必须都在group by分组条件内(含有函数的字段除外)。(如果遇到order by也出现这个问题,同理,order by字段也都要在group by内)。
解决方法:去my.cnf 或者 my.ini中 [mysqld]下增加以下语句:
## 解决only_full_group_by的问题
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
如果没有my.cnf,比如博主的电脑是mac,那么就请查看另一篇文章。
转载一份别人的测试:
首先创建数据库hncu,建立stud表格。
添加数据:
create table stud(
sno varchar(30) not null primary key,
sname varchar(30) not null,
age int,
saddress varchar(30)
);INSERT INTO stud VALUES('1001','Tom',22,'湖南益阳');INSERT INTO stud VALUES('1002','Jack',23,'益阳');INSERT INTO stud VALUES('1003','李白',22,'益阳');INSERT INTO stud VALUES('1004','王五',24,'中国北京');INSERT INTO stud VALUES('1005','张三',22,'益阳');INSERT INTO stud VALUES('1006','张四',23,'益阳');INSERT INTO stud VALUES('1007','李四',22,'湖南益阳');INSERT INTO stud VALUES('1008','刘备',24,'北京');1234567891011121314
执行语句如下:
SELECT * FROM stud GROUP BY saddress;1
显示了如下错误:
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'hncu.stud.sno' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by12
再执行此句:
SELECT saddress as 平均年龄 FROM stud GROUP BY saddress;1
-没有问题
然后我们用MySQL,再执行前面那句错误的代码:
也就是:
SELECT * FROM stud GROUP BY saddress;1
我们看结果:
顺利的通过了,但是,你发现没有,前面的smo,sname,age,这3列的数据不对啊,没错,MySQL强行显示第一次查找到的saddress不同的行了!!!其实这个结果是不对,但是MySQL应该是兼容了这个错误!
而DOS却是严格按照SQL的语法来的。
SQL的grop by 语法为,
select 选取分组中的列+聚合函数 from 表名称 group by 分组的列
从语法格式来看,是先有分组,再确定检索的列,检索的列只能在参加分组的列中选。
所以问题中的,group by 后的 a,b,c是先确定的。select后的a,b,c才是可以变的。即
以下语句都是正确的:
select a,b,c from table_name group by a,b,c,d;select a,b from table_name group by a,b,c;select a,max(a) from table_name group by a,b,c;123
以下语句则是错误的:
select a,b,c from table_name group by a,b;select a,b,c from table_name group by a;12
而因为MySQL的强大,它兼容了这个错误!!!
但是在DOS是不能的。所以出现了DOS下报错,而在MySQL中能够查找的情况(其实这个查找的结果是不对的)。
1 +1
版权声明
分享到:
发表评论
请文明留言
发表
共 0 条评论