mysql5.7先排序ORDERBY,后分组GROUPBY人工智能+python培训
关于mysql5.7  排序ORDER BY ,分组 GROUP BY
w3cschool手机版本可以编程吗安装最新的MySQL5.7,发现之前group by语句报错,发现mysql5.7 默认开启 only_full_group_by 模式,
错误信息如下:
[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column
升序和降序desc'der_no' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by
解决⽅式:
第⼀种⽅式:直接修改配置⽂件删掉only_full_group_by
mysql语句顺序select 标签第⼆种⽅式:
set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_上⾯sql改变全局sql_mode,新建的数据库有效
小程序开发平台开源
set sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_这条sql修改已存在的数据库sql_mode
然后开始执⾏先排序后分组的sql
SELECT * FROM
(SELECT * FROM users ORDER BY user_id DESC) temp
GROUP BY temp. user_id
发现排序居然没有⽣效,顿时很惊讶
explain 查看执⾏计划,发现在没有 limit 的情况,会少了⼀个derived 操作,mysql会认为这时候不需要排序,内部做了优化,所以这种情况下加limit限制就可以了,如下(limt 后的⾏数 可以作为参数传⼊):
SELECT * FROM
(SELECT * FROM users ORDER BY user_id DESC limit 100) temp
GROUP BY temp. user_id

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。