报错语句
select distinct order_num, prod_id from orderitems order by order_item;
报错详情
ERROR 3065 (HY000): Expression #1 of ORDER BY clause is not in SELECT list, references column 'Mydb.orderitems.order_item' which is not in SELECT list; this is incompatible with DISTINCT
报错原因
- mysql中distinc执行顺序高于order by
- distinct查询时产生一张虚拟临时表
- order by在虚拟临时表中找不到排序字段,因而报错
参考
mysql distinct和order by 一起用时,order by的字段必须在select中
mysql Unknown error 3065
解决方案
mysql5.7 DISTINCT 后 order by 报错:[3065] 快速解决方案
本文解析了在MySQL中使用DISTINCT和ORDER BY时遇到的3065错误,重点讲解了原因——DISTINCT操作创建临时表导致orderby字段缺失。提供了解决方案,包括确保orderby字段在SELECT列表中的必要性,适用于5.7版本的快速修复方法。
4837

被折叠的 条评论
为什么被折叠?



