references column 'xxx' which is not in SELECT list

具体问题:


问题分析:

    通过查阅资料发现在mysql5.7.5及以上版本实现了对功能依赖的检测。默认启用了ONLY_FULL_GROUP_BY SQL模式

1.在该模式下,我们使用GROUP BY查询时,出现在SELECT字段后面的只能是GROUP BY后面的分组字段,或使用聚合函数包裹着的字段,否则会报错如下信息:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'database.table.column' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

2.当使用ORDER BY查询时,不能使用SELECT DISTINCT去重查询。否则会报错如下信息:Expression #1 of ORDER BY clause is not in SELECT list, references column 'database.table.column' which is not in SELECT list; this is incompatible with DISTINCT


查看mysql版本命令

select VERSION()

 查看session和global默认的功能依赖

SHOW SESSION VARIABLES where Variable_name = 'sql_mode';

SHOW GLOBAL VARIABLES where Variable_name = 'sql_mode';

select @@sql_mode;

解决方案:

1.通过命令关闭ONLY_FULL_GROUP_BY SQL模式(暂时的,下次重启mysql失效)

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';

2.通过修改mysql的配置文件关闭ONLY_FULL_GROUP_BY SQL模式(永久的)

[mysqld]
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

                                                                                                          我太难了~

原创文章 15 获赞 8 访问量 1万+

猜你喜欢

转载自blog.csdn.net/shujudaniu/article/details/100042259