为什么抵消变化,结果是一样的?

时间:2016-01-26 10:28:50

标签: mysql

执行查询时

select * from broker_commission_rate 
where broker_commission_rate.deleted_at is null 
order by settlement_at asc limit 20 offset 520

我的结果以560开头,以ID 584结尾。

当我将偏移从520更改为540时,我得到相同的结果集。

select * from broker_commission_rate 
where broker_commission_rate.deleted_at is null 
order by settlement_at asc limit 20 offset 540

将order by子句更改为order by settlement_at asc,id asc,结果集已更改。

我想知道为什么offset在这个SQL中不起作用?提前谢谢。

表格定义:

CREATE TABLE `broker_commission_rate` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `sn` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `broker_uid` int(11) NOT NULL DEFAULT '0' ,
  `broker_type` int(11) NOT NULL DEFAULT '0' ,
  `broker_level` int(11) NOT NULL DEFAULT '0',
  `employee_id` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `type` tinyint(4) NOT NULL DEFAULT '0' ,
  `rate` int(11) NOT NULL DEFAULT '0' ,
  `price` decimal(8,2) NOT NULL DEFAULT '0.00' ,
  `broker_price` decimal(8,2) NOT NULL DEFAULT '0.00' ,
  `settlement_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `broker_commission_rate_sn_index` (`sn`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

系统信息:

Mysql 5.6.20 @ Linux x85_64

1 个答案:

答案 0 :(得分:0)

偏移确实有效!

正如您所说,当您更改order by子句时,它会返回准确的数据集。这意味着您的条件顺序不正确。

您按settlement_at排序,此列中可能存在重复值,因此当您添加另一个条件按id排序时。它运作良好。

运行方式如下:如果settlement_at相同,请查找id进行排序。

所以你的最终查询应该是:

select * 
from broker_commission_rate 
where broker_commission_rate.deleted_at is null 
order by settlement_at asc, id asc 
limit 20 offset 540