MySql下实现先排序后分组
对比可以发现5.7版本的MySql在执行这条sql时缺少了一个derived操作,通过查阅相关资料了解到MySql 5.7对子查询进行了优化,认为子查询中的order by可以进行忽略,只要Derived table里不包含如下条件就可以进行优化: UNION clause GROUP BY DISTINCT Aggregation LIMIT or OFFSET 这里把链接放上:5.7中Derived table变形记 最后放上相应的解决办法: --方法一,仅适用于低于5.7版本的MySql-- select * from (select * from shop order by price desc) a GROUP BY a.shop_name; --方法二-- select * from (select * from shop order by price desc limit 999999) a GROUP BY a.shop_name; --方法三-- select * from shop a where N > (select count(*) from shop b where b.shop_name = a.shop_name and a.price < b.price) order by a.shop_name,a.price desc; 1 2 3 4 5 6