您的位置:首页 > 数据库 > MySQL

mysql中排序再分页遇到的重复数据

2016-11-30 22:50 369 查看
  用一个简单实例,对遇到的问题复盘一下。

  1.新建测试表 test_ordercreate table test_order(
id int(11) not null auto_increment primary key,
name varchar(10) not null,
create_time datetime not null,
state int(1) default '1'
)
  2.插入测试数据

-- ----------------------------
-- Records of test_order
-- ----------------------------
INSERT INTO `test_order` VALUES ('1', '张三', '2016-11-29 08:10:10', '1');
INSERT INTO `test_order` VALUES ('2', '李四', '2016-11-30 12:10:10', '1');
INSERT INTO `test_order` VALUES ('3', '赵一', '2016-11-30 14:10:10', '1');
INSERT INTO `test_order` VALUES ('4', '钱二', '2016-11-29 14:00:00', '1');
INSERT INTO `test_order` VALUES ('5', '周七', '2016-11-29 14:00:00', '1');
INSERT INTO `test_order` VALUES ('6', '郑八', '2016-11-29 14:00:00', '1');
INSERT INTO `test_order` VALUES ('7', '王五', '2016-11-29 14:00:00', '1');
INSERT INTO `test_order` VALUES ('8', '刘六', '2016-11-29 14:00:00', '1'); 3.查询数据(排序并分页)
select name from test_order where state = 1 ORDER BY create_time limit 0, 4 结果为:



select name from test_order where state = 1 ORDER BY create_time limit 4, 4
结果为:



这2条sql都查出“刘六”出来了,出现重复的数据。

原因:由于order by 字段重复的数据导致的,建议将order by 后面的字段使用唯一的字段,或者使用2个字段组成唯一的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: