mysql5.7 和mysql 5.6 group by 差异

#表的结构 test3
CREATE TABLE IF NOT EXISTS test3 (
id int(11) NOT NULL auto_increment,
bid int(11) NOT NULL,
cid int(11) NOT NULL,
dtime datetime NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

#转存表中的数据 test3

INSERT INTO test3 (id, bid, cid, dtime) VALUES
(1, 1, 3, ‘2014-03-18 16:00:00’),
(2, 1, 10, ‘2014-03-18 17:00:00’),
(3, 2, 5, ‘2014-03-18 18:00:00’),
(4, 2, 6, ‘2014-03-18 19:00:00’),
(5, 1, 7, ‘2014-03-18 20:00:00’);

二、通过子查询实现

1、sql语句

select from(select from test3 ORDER BY dtime DESC) as temp GROUP BY bid ORDER BY dtime DESC;

那么问题来了

有的电脑bid = 1组 5.7mysql是16点,5.67mysql是20点,就是说相反的

通过 explain 查看执行计划,可以看到没有 limit 的时候,少了一个 DERIVED 操作估计是内部优化了,认为 ORDER BY 在这种语法中可忽略有 LIMIT 限制涉及排序后的结果,不会忽略 ORDER BY,可以达到预期
测试了一下
select from(select from test3 ORDER BY dtime DESC limit 999999999) as temp GROUP BY bid ORDER BY dtime
可以,但是不太优雅
官网资料
https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
根据官网的描述
还可以这样
select from(select DISTINCT from test3 ORDER BY dtime DESC ) as temp GROUP BY bid ORDER BY id desc