mysql5.7中group by和mysql5.5中group by的結果不一樣
問題描述
在5.7和5.5中同樣的sql語句,執行的結果不一樣。主要就是想要取每個分組中id最大記錄。語句如下:
select t2.* from (select t1.* from t_user t1 order by t1.id desc) as t2 group by t2.type;
5.7的結果:
5.5的結果:
sql腳本如下:
DROP TABLE IF EXISTS `t_user`;CREATE TABLE `t_user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `phone` varchar(255) DEFAULT NULL, `gender` varchar(255) DEFAULT NULL, `type` varchar(255) DEFAULT NULL, `birth` datetime DEFAULT NULL, `is_delete` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8;-- ------------------------------ Records of t_user-- ----------------------------INSERT INTO `t_user` VALUES (’1’, ’James’, ’0594-5397864’, ’0’, ’3’, ’2016-01-30 19:01:09’, ’1’);INSERT INTO `t_user` VALUES (’2’, ’Hayes’, ’0594-5392419’, ’1’, ’4’, ’2015-12-24 11:12:27’, ’1’);INSERT INTO `t_user` VALUES (’3’, ’Diana’, ’0594-5393520’, ’1’, ’5’, ’2016-03-21 13:03:50’, ’0’);INSERT INTO `t_user` VALUES (’4’, ’Rajah’, ’0594-5399812’, ’1’, ’4’, ’2015-11-26 02:11:35’, ’0’);INSERT INTO `t_user` VALUES (’5’, ’Daria’, ’0594-5397571’, ’0’, ’4’, ’2016-01-18 11:01:11’, ’1’);INSERT INTO `t_user` VALUES (’6’, ’Lee’, ’0594-5394539’, ’1’, ’1’, ’2015-10-23 08:10:23’, ’1’);INSERT INTO `t_user` VALUES (’7’, ’Cameran’, ’0594-5392867’, ’0’, ’4’, ’2016-11-16 12:11:08’, ’0’);INSERT INTO `t_user` VALUES (’8’, ’Wylie’, ’0594-5395349’, ’0’, ’5’, ’2017-07-06 04:07:27’, ’0’);INSERT INTO `t_user` VALUES (’9’, ’Bertha’, ’0594-5395287’, ’1’, ’1’, ’2017-02-08 12:02:45’, ’1’);INSERT INTO `t_user` VALUES (’10’, ’Fletcher’, ’0594-5399246’, ’0’, ’4’, ’2015-09-03 20:09:33’, ’0’);INSERT INTO `t_user` VALUES (’11’, ’Conan’, ’0594-5391546’, ’1’, ’5’, ’2017-05-15 09:05:23’, ’0’);INSERT INTO `t_user` VALUES (’12’, ’Raymond’, ’0594-5399666’, ’0’, ’3’, ’2015-10-20 05:10:05’, ’1’);INSERT INTO `t_user` VALUES (’13’, ’Noel’, ’0594-5397392’, ’1’, ’4’, ’2017-05-26 03:05:56’, ’0’);INSERT INTO `t_user` VALUES (’14’, ’Miriam’, ’0594-5399081’, ’0’, ’2’, ’2016-05-21 02:05:09’, ’0’);INSERT INTO `t_user` VALUES (’15’, ’Maya’, ’0594-5397242’, ’0’, ’3’, ’2016-10-24 02:10:50’, ’1’);INSERT INTO `t_user` VALUES (’16’, ’Winifred’, ’0594-5395142’, ’1’, ’1’, ’2017-03-15 02:03:43’, ’0’);INSERT INTO `t_user` VALUES (’17’, ’Elaine’, ’0594-5398478’, ’1’, ’3’, ’2017-03-08 15:03:03’, ’1’);INSERT INTO `t_user` VALUES (’18’, ’Robert’, ’0594-5397830’, ’0’, ’5’, ’2016-02-10 22:02:06’, ’0’);INSERT INTO `t_user` VALUES (’19’, ’Patrick’, ’0594-5396516’, ’0’, ’4’, ’2015-09-10 07:09:51’, ’0’);INSERT INTO `t_user` VALUES (’20’, ’Darrel’, ’0594-5397417’, ’0’, ’1’, ’2016-03-11 11:03:36’, ’0’);INSERT INTO `t_user` VALUES (’21’, ’Salvador’, ’0594-5399732’, ’1’, ’3’, ’2016-01-01 15:01:21’, ’0’);INSERT INTO `t_user` VALUES (’22’, ’Brandon’, ’0594-5396204’, ’1’, ’4’, ’2016-05-12 06:05:40’, ’1’);INSERT INTO `t_user` VALUES (’23’, ’Dorothy’, ’0594-5396783’, ’0’, ’1’, ’2016-12-12 10:12:59’, ’1’);INSERT INTO `t_user` VALUES (’24’, ’Kevyn’, ’0594-5398240’, ’0’, ’2’, ’2016-02-07 04:02:14’, ’1’);INSERT INTO `t_user` VALUES (’25’, ’Brody’, ’0594-5398774’, ’1’, ’1’, ’2016-12-11 20:12:36’, ’0’);
問題解答
回答1:select max(t2.id), t2.name, t2.phone, t2.gender, t2.type, t2.birth, t2.is_delete from t_user t2 group by t2.name, t2.phone, t2.gender, t2.type, t2.birth, t2.is_delete;
語法錯誤,mysql對group by沒那么敏感,你換oracle就知道了
回答2:事實上,Mysql 并沒有做錯什么。
同樣的數據,存儲的先后順序、算法的版本不同,算出的結果都有可能會不一樣。但是其實這兩個版本的結果都是滿足你的 SQL 的要求的。
這就跟無序列表取第一個值一樣,不同是實現返回的結果可能不一樣,能保證的就是肯定在這個列表內。
如果你需要相同的結果,那就把 SQL 寫得嚴一點以至于只可能有唯一的結果滿足你的條件。
相關文章:
1. centos - apache配置django報錯:cannot be loaded as Python modules2. 微信端電子書翻頁效果3. node.js - 有沒有比較好的nodejs導出excel的插件?4. css3 - 微信前端頁面遇到的transition過渡動畫的bug5. mysql - SQL問個基礎例子,書上的,我怎么看都看不懂..誰幫我解釋一下第2個為什么和第1個一樣?6. javascript - 微信小程序里怎么把頁面轉成圖片分享7. mysql事務日志的一些問題8. mysql服務無法啟動1067錯誤,誰知道正確的解決方法?9. mysql - 我用SQL語句 更新 行的時候,發現全部 中文都被清空了,請問怎么解決?10. 數據庫 - mysql boolean型無法插入true
