Mysql的Bug之一:unexpected results using limit in combination with none unique field sorting
不过这里定性为bug但是许多的评论说并不认为是一个bug,如果确实要让得到结果最终成为预料之中的结果的话,需要将order by 后面的排序条件尽量详细使得排序的结果本身不要存在这种可能。
案例解析
建表语句
CREATE TABLE `TestCase2` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`aValue` decimal(19,2) NOT NULL,
`accuracyClassType_id` bigint(20) NOT NULL,
`productType_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `FKF58064BD791396` (`productType_id`),
KEY `FKF58064BDED5076` (`accuracyClassType_id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
填充数据
INSERT INTO `TestCase2` (`id`, `aValue`, `accuracyClassType_id`, `productType_id`)
VALUES
(1,2.00,3,2),
(2,3.00,3,2),
(3,4.00,2,3),
(4,5.00,2,3),
(5,6.00,2,3),
(6,8.00,2,3),
(7,10.00,2,3),
(8,12.00,2,3),
(9,16.00,2,3),
(10,20.00,2,3),
(11,6.00,2,4),
(12,8.00,2,4),
(13,10.00,2,4),
(14,12.00,2,4),
(15,5.00,2,2),
(16,6.00,2,2);
查询语句
select * from TestCase2 order by aValue desc limit 4;
select * from TestCase2 order by aValue desc limit 3;
解决方案
select * from TestCase2 order by aValue ,id limit 4;
Bug地址
unexpected results using limit in combination with none unique field sorting
另外
数据库版本为5.5的Mysql经过测试并不存在上述问题。