MariaDB按日期排序并限制偏移和缺少结果

时间:2016-06-13 15:44:32

标签: mysql mariadb

我有一张这个DDL的表:

CREATE TABLE BAR_INGRESO_TEST (
  ING_ID BIGINT(20) PRIMARY KEY NOT NULL AUTO_INCREMENT,
  ING_DESCRIPCION VARCHAR(255),
  ING_FECHA DATE,
  ING_IMPORTE DECIMAL(19,2),
  ING_SOCIO_ID BIGINT(20)
);

这个样本数据:

INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 1', '2015-10-22', 73.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 2', '2015-10-29', 68.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 3', '2015-11-26', 190.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 4', '2015-12-09', 175.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 5', '2015-12-16', 200.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 6', '2015-12-18', 395.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 7', '2015-12-18', 161.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 8', '2015-12-30', 246.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 9', '2016-01-03', 145.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 10', '2015-12-21', 46.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 11', '2015-12-21', 73.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 12', '2016-02-03', 69.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 13', '2016-02-03', 37.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 14', '2016-02-03', 25.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 15', '2016-03-07', 50.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 16', '2016-04-18', 92.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 17', '2016-04-18', 74.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 18', '2016-06-03', 210.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 19', '2015-07-06', 75.00, 15);
INSERT INTO BAR_INGRESO_TEST (ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID) VALUES ('Description 20', '2015-07-06', 25.00, 15);

此查询提供以下结果:

select * from BAR_INGRESO_TEST order by ING_FECHA desc;

+--------+-----------------+------------+-------------+--------------+
| ING_ID | ING_DESCRIPCION | ING_FECHA  | ING_IMPORTE | ING_SOCIO_ID |
+--------+-----------------+------------+-------------+--------------+
|     18 | Description 18  | 2016-06-03 |      210.00 |           15 |
|     17 | Description 17  | 2016-04-18 |       74.00 |           15 |
|     16 | Description 16  | 2016-04-18 |       92.00 |           15 |
|     15 | Description 15  | 2016-03-07 |       50.00 |           15 |
|     14 | Description 14  | 2016-02-03 |       25.00 |           15 |
|     13 | Description 13  | 2016-02-03 |       37.00 |           15 |
|     12 | Description 12  | 2016-02-03 |       69.00 |           15 |
|      9 | Description 9   | 2016-01-03 |      145.00 |           15 |
|      8 | Description 8   | 2015-12-30 |      246.00 |           15 |
|     10 | Description 10  | 2015-12-21 |       46.00 |           15 |
|     11 | Description 11  | 2015-12-21 |       73.00 |           15 |
|      7 | Description 7   | 2015-12-18 |      161.00 |           15 |
|      6 | Description 6   | 2015-12-18 |      395.00 |           15 |
|      5 | Description 5   | 2015-12-16 |      200.00 |           15 |
|      4 | Description 4   | 2015-12-09 |      175.00 |           15 |
|      3 | Description 3   | 2015-11-26 |      190.00 |           15 |
|      2 | Description 2   | 2015-10-29 |       68.00 |           15 |
|      1 | Description 1   | 2015-10-22 |       73.00 |           15 |
|     19 | Description 19  | 2015-07-06 |       75.00 |           15 |
|     20 | Description 20  | 2015-07-06 |       25.00 |           15 |
+--------+-----------------+------------+-------------+--------------+

直到这里一切都很好。问题是这两个问题:

select * from BAR_INGRESO_TEST order by ING_FECHA desc limit 10;

select * from BAR_INGRESO_TEST order by ING_FECHA desc limit 10, 10;

我需要对这些结果进行分页,但始终缺少此条目:

|     10 | Description 10  | 2015-12-21 |       46.00 |           15 |

我的桌子或查询有什么问题?

1 个答案:

答案 0 :(得分:2)

您的查询存在的问题是您的表格在10, 11个地方包含以下记录:

ING_ID, ING_DESCRIPCION, ING_FECHA, ING_IMPORTE, ING_SOCIO_ID
--------------------------------------------------------------
10,     Description 10,  2015-12-21, 46.00,      15
11,     Description 11,  2015-12-21, 73.00,      15

恰好具有相同的 ING_FECHA值。两个查询都选择相同的记录,即具有ING_ID=11的记录。

要解决此问题,您需要在ORDER BY子句中指定第二个字段:

select * 
from BAR_INGRESO_TEST 
order by ING_FECHA desc, ING_ID limit 10;