mySQL将下一个和前一个日期记录到当前行的日期

时间:2013-05-11 18:02:53

标签: mysql

我有一个带有日期列的表,我用它来查询以从中获取一组行。

我没有存储任何时间值只是日期和T:00:00:00

我需要锻炼我有数据的下一天和前几天。

我希望能够通过查询选择当前数据集来执行单个查询和日期以使用下一个和后一个UI导航

所以从这开始获取最后一组数据

SELECT * 
FROM  `newco_bh_price_data` 
WHERE  `manager_id` =960
AND  `currency_id` =0
AND  `class_id` =  'A'
AND  `price_date` 
IN (SELECT MAX(  `price_date` ) 
FROM  `newco_bh_price_data`
)

我现在需要知道下一个日期是什么,以便我可以查询

SELECT * 
FROM  `newco_bh_price_data` 
WHERE  `manager_id` =960
AND  `currency_id` =0
AND  `class_id` =  'A'
AND  `price_date` = '2002-09-19'

感谢您提供任何帮助和意见

以下是数据样本

id  price_date  manager_id  currency_id class_id    value   v_change
554 2002-09-19  960 0   A   8.631   0
553 2002-09-19  960 0   A   9.5635  0
552 2002-09-18  960 0   A   9.1068  0
551 2002-09-18  960 0   A   8.9351  0
550 2002-09-18  960 0   A   9.61    0
549 2002-09-17  960 0   A   9.1868  0
548 2002-09-17  960 0   A   9.0201  0
547 2002-09-17  960 0   A   9.6425  0
546 2002-09-16  960 0   A   9.3377  0
545 2002-09-16  960 0   A   9.3317  0
544 2002-09-16  960 0   A   9.6902  0
543 2002-09-13  960 0   A   9.418   0
542 2002-09-13  960 0   A   9.3669  0
541 2002-09-13  960 0   A   9.7125  0
540 2002-09-12  960 0   A   9.4463  0
539 2002-09-12  960 0   A   9.4383  0
538 2002-09-12  960 0   A   9.7474  0
537 2002-09-11  960 0   A   9.5303  0
536 2002-09-11  960 0   A   9.6592  0
535 2002-09-11  960 0   A   9.8041  0
534 2002-09-10  960 0   A   9.4666  0
533 2002-09-10  960 0   A   9.5878  0
532 2002-09-10  960 0   A   9.7651  0
531 2002-09-09  960 0   A   9.3104  0
530 2002-09-09  960 0   A   9.4841  0
529 2002-09-09  960 0   A   9.7498  0
528 2002-09-06  960 0   A   9.2462  0
527 2002-09-06  960 0   A   9.3823  0
526 2002-09-06  960 0   A   9.7501  0
525 2002-09-05  960 0   A   9.0465  0

2 个答案:

答案 0 :(得分:0)

您真正想要的功能是lag()lead()。但是,唉,这些在MySQL中不可用。我认为实现它们的最简单方法是使用相关子查询。

以下是一个例子:

SELECT *,
       (select price_date
        from newco_bh_price_data pd1
        where pd1.manager_id = pd.manager_id and
              pd1.currency_id = pd.currency_id and
              pd1.class_id = pd.class_id and
              pd1.price_date > pd.price_date
        order by price_date
        limit 1
       ) as next_price_date,
       (select price_date
        from newco_bh_price_data pd1
        where pd1.manager_id = pd.manager_id and
              pd1.currency_id = pd.currency_id and
              pd1.class_id = pd.class_id and
              pd1.price_date < pd.price_date
        order by price_date desc
        limit 1
       ) as prev_price_date
FROM  `newco_bh_price_data` pd
WHERE  `manager_id` =960
AND  `currency_id` =0
AND  `class_id` =  'A'
AND  `price_date` = '2002-09-19';

这假设您在查找下一个和上一个日期时希望经理,货币和班级相同。

答案 1 :(得分:0)

查询:

<强> SQLFIDDLEExample

SELECT t1.*,
       (SELECT MIN(t2.`price_date`)
        FROM `newco_bh_price_data` t2
        WHERE t2.`price_date`> t1.`price_date`
        AND t2.`manager_id`= t1.`manager_id`
        AND t2.`currency_id` = t1.`currency_id`
        AND t2.`class_id` = t1.`class_id`
        ) AS NextDate,
       (SELECT MAX(t2.`price_date`)
        FROM `newco_bh_price_data` t2
        WHERE t2.`price_date`< t1.`price_date`
        AND t2.`manager_id`= t1.`manager_id`
        AND t2.`currency_id` = t1.`currency_id`
        AND t2.`class_id` = t1.`class_id`
        ) AS PreviousDate
FROM  `newco_bh_price_data` t1
WHERE  t1.`manager_id` =960
AND  t1.`currency_id` =0
AND  t1.`class_id` =  'A'

结果:

|  ID |                       PRICE_DATE | MANAGER_ID | CURRENCY_ID | CLASS_ID | VALUE | V_CHANGE |                         NEXTDATE |                     PREVIOUSDATE |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 554 | September, 19 2002 00:00:00+0000 |        960 |           0 |        A |     9 |        0 |                           (null) | September, 18 2002 00:00:00+0000 |
| 553 | September, 19 2002 00:00:00+0000 |        960 |           0 |        A |    10 |        0 |                           (null) | September, 18 2002 00:00:00+0000 |
| 552 | September, 18 2002 00:00:00+0000 |        960 |           0 |        A |     9 |        0 | September, 19 2002 00:00:00+0000 | September, 17 2002 00:00:00+0000 |
| 551 | September, 18 2002 00:00:00+0000 |        960 |           0 |        A |     9 |        0 | September, 19 2002 00:00:00+0000 | September, 17 2002 00:00:00+0000 |
| 550 | September, 18 2002 00:00:00+0000 |        960 |           0 |        A |    10 |        0 | September, 19 2002 00:00:00+0000 | September, 17 2002 00:00:00+0000 |
| 549 | September, 17 2002 00:00:00+0000 |        960 |           0 |        A |     9 |        0 | September, 18 2002 00:00:00+0000 | September, 16 2002 00:00:00+0000 |
| 548 | September, 17 2002 00:00:00+0000 |        960 |           0 |        A |     9 |        0 | September, 18 2002 00:00:00+0000 | September, 16 2002 00:00:00+0000 |