MYSQL语句页面报错

页面截图:
image.png
image.png
相关代码
页面跳转报错:
SQL语句执行错误:select a.* from (select album, addtime, id from blog_kl_album order by album, addtime desc, id desc) a group by album
Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'a.addtime' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

«点击返回;)

image.png

求助,怎么写能正常运行呢,MYSQL版本5.7.14

阅读 2.4k
2 个回答

在高版本的的 MySQL 中,严格了当使用 group by 时,column 必须出现在 group by 后面或者,使用聚合函数。

解决方法

  • 报错的原因是在 only_full_group_by sql mode 下,mysql 不允许在 select list 中存在未在 GROUP BY 子句中引用的非聚合列。
  • 这句 sql 如何写要看你的业务逻辑了,比如有两条数据:
album addtime id
foo 1574956800 1
foo 1574870400 2

album 相同是,如果处理 addtime, id 两个列的值,取最大值(MAX)? 随机一个值(ANY_VALUE)?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题