ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause ... 的错误

在mysql中使用 group by 出现的问题 

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

问题原因: 
ONLY_FULL_GROUP_BY的意思是:对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中,也就是说查出来的列必须在group by后面出现,否则就会报错,或者这个字段出现在聚合函数里面。 

在select指定的字段要么就要包含在Group By语句的后面,作为分组的依据;要么就要被包含在聚合函数中。

ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause ... 的错误_第1张图片

ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause ... 的错误_第2张图片  

找原因:

查看mysql版本命令:select version(); 

查看sql_model参数命令: 
SELECT @@GLOBAL.sql_mode;

SELECT @@SESSION.sql_mode; 

ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause ... 的错误_第3张图片

查看:
 ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
第一项默认开启ONLY_FULL_GROUP_BY了,导致出现了错误,需要把它的默认关掉。

解决方法: 
命令行输入 :
 set sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

结果:

ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause ... 的错误_第4张图片

你可能感兴趣的:(MySql)