因为换电脑了,所以重新搭了一次环境,查询数据库的时候报错:
Expression #1 of ORDER BY clause is not in SELECT list, references column ‘a.cook.sort’ which is not in SELECT list; this is incompatible with DISTINCT
查了一下具体原因:
mysql5.7.5及以上版本将sql_mode的ONLY_FULL_GROUP_BY模式默认设置为打开状态,会导致一些错误:
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;
暂时解决办法: https://www.cnblogs.com/start-fxw/p/11882230.html.
但是为什么不建议用呢?之前关闭也没啥问题?关闭会有啥影响呢?
其实官方已经给了很详细的解释
1.如下图的address 分组的时候address会出现不一样的数据,这时候就不知道该显示哪个数据了,我之前也遇到过这种问题,想按创建时间排序取第一个但是不生效,看到这就明白了!
2.第二个问题时distinct和order by连用 select distinct c1,c2 from t order by c3;这种情况下结果也是矛盾的
官网: https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html.