mysql分组条件和查询结果不同,如何解决?

sql:

//查询结果比分组多报错
 select  rong_liang,yong_liang,`level`,adapt_niandu,perrfect_niandu,
 oil_type,fadongji,GROUP_CONCAT(mid) as mids,mid
 from datum_copy3 WHERE rong_liang != "" and  rong_liang != 
 "电动车" and oil_type != "" and perrfect_niandu !=""
 AND `level` != "" AND  fadongji != ""
 group by rong_liang,yong_liang,`level`,adapt_niandu,
 perrfect_niandu,oil_type,fadongji;

sql提示: Expression #9 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘f6dos.datum_copy3.mid’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by, Time: 0.000000s

这肯定是mysql的sql_mode配置问题!
通过 sql查询默认的格式
select @@global.sql_mode
结果: 必须是所有分组条件,
在这里插入图片描述

解决

set @@global.sql_mode 
="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION";

然而并没有成功!!!

又查了下

SELECT @@SESSION.sql_mode;

发现还有only_full_group_by。

重新设置@@SESSION.sql_mode的值去掉,only_full_group_by

set @@global.sql_mode 
="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION";

完美解决:

总结:

-- 需要看两个属性
select @@global.sql_mode;
SELECT @@SESSION.sql_mode;
-- 如果都有only_full_group_by去掉即可
set @@SESSION.sql_mode
="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION";
set @@global.sql_mode 
="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION";

你可能感兴趣的:(mysql,mysql)