Expression #1 of ORDER BY clause is not in SELECT list, references column 'xxxx' which is not in SELECT list; this is incompatible with DISTINCT
select DISTINCT(left(created_on, 10)) createDate from record where plan_id = '123456789' order by created_on desc;
报错信息:
Expression #1 of ORDER BY clause is not in SELECT list,
references column 'xxxx' 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
‘xxx’ which is not functionally dependent on columns
in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by
详见:https://www.cnblogs.com/wongzzh/p/15303934.html
2、当使用ORDER BY查询时,不能使用SELECT
DISTINCT去重查询。否则会报错如下信息: Expression #1 of ORDER BY clause is not in
SELECT list, references column ‘xxx’ which is not in
SELECT list; this is incompatible with DISTINCT
解决办法:
select DISTINCT(left(created_on, 10)) createDate from record where plan_id = '123456789' ;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本