mysql报错:Expression #1 of ORDER BY clause is not in SELECT list

报错

SELECT
	DISTINCT n.id,
	n.`status`,
	n.title,
	n.content 
FROM
	notice n
WHERE
	n.STATUS = 2 
ORDER BY
	n.release_time DESC

3065 - Expression #1 of ORDER BY clause is not in SELECT list, references column ‘xinyuan_server.n.release_time’ which is not in SELECT list; this is incompatible with DISTINCT

解决办法

第一种
 # 去重方式使用 group by 分组去重
SELECT n.id,
               n.`status`,
               n.title,
               n.content
        FROM notice n
                 LEFT JOIN notice_scope ns on ns.notice_id = n.id
        WHERE n.STATUS = 2
          and ns.project_id = '1'
        GROUP BY n.id
        ORDER BY n.release_time DESC
第二种

参考 https://blog.csdn.net/qq_25983579/article/details/104890672

问题原因:
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; this is incompatible with DISTINCT

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
\[1\]这个错误信息是在使用ORDER BY子句时出现的。它表示在ORDER BY子句中引用的列不在SELECT列表中,这与使用DISTINCT关键字不兼容。\[2\]这个错误通常是由于MySQL的sql_mode设置中的ONLY_FULL_GROUP_BY模式导致的。在这种模式下,使用GROUP BY查询时,SELECT字段后面只能是GROUP BY后面的分组字段或使用聚合函数包裹的字段。否则,会出现上述错误信息。同样,当使用ORDER BY查询时,不能与SELECT DISTINCT一起使用。\[3\]sql_mode是MySQL的一个配置选项,它定义了MySQL在执行查询时的行为。其中包括ONLY_FULL_GROUP_BY模式,它会导致上述错误。如果你想避免这个错误,可以修改sql_mode的设置,将ONLY_FULL_GROUP_BY模式关闭。 #### 引用[.reference_title] - *1* *2* [Expression #1 of ORDER BY clause is not in SELECT list,references column 'xxxxxxx'](https://blog.csdn.net/hanshanyunhai/article/details/105701771)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Caused by: java.sql.SQLException: Expression #1 of ORDER BY clause is not in SELECT list, reference](https://blog.csdn.net/weixin_43232423/article/details/112507291)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值