得到日期之间的日子

时间:2014-09-14 03:33:33

标签: php sqlanywhere

当我执行这个sql代码时:

    SELECT 
        ROW_NUMBER() OVER (ORDER BY schdate), 
        ACCT , 
        SCHDATE AS DATES,
        NULL  AS ORS 
        FROM AMORT 
        WHERE ACCT = '0000360' AND CYCLE = '0001' AND principal <> '0.00'
    UNION ALL
    SELECT 
         ROW_NUMBER() OVER (ORDER BY ddate), 
         ACCT, 
         DDATE AS DATES,
         OR_NO AS ORS 
         FROM LEDGER 
         WHERE (ACCT = '0000360' AND CYCLE = '0001') AND (t_code = '10' OR t_code = '11' OR t_code = '12' OR t_code = '13')
     ORDER BY DATES

我已经得到了这个结果。但是我如何才能完成日期之间的日子?

ACCT    |     DATES |      ORS
-------------------------------------
0000360 |2014-07-26 | (NULL)
0000360 |2014-08-02 | (NULL)
0000360 |2014-08-05 | 011290000113
0000360 |2014-08-09 | (NULL)
0000360 |2014-08-16 | (NULL)
0000360 |2014-08-19 | 011290000559

我希望得到日​​期之间的日子,如下图所示。

ACCT    |     DATES |      ORS
-------------------------------------
======================
0000360 |2014-07-26 || (NULL)
0000360 |2014-08-02 || (NULL)
======================
0000360 |2014-08-05 | 011290000113
======================
0000360 |2014-08-09 || (NULL)
0000360 |2014-08-16 || (NULL)
======================
0000360 |2014-08-19 | 011290000559

获取没有ORS的日期并计算当天之间的天数,例如:

ACCT    |     DATES |      ORS
-------------------------------------
======================
0000360 |2014-07-26 || (NULL)
0000360 |2014-08-02 || (NULL)
======================

因此,在2014-07-26和2014-08-02之间,计算天数是7

======================
0000360 |2014-08-09 || (NULL)
0000360 |2014-08-16 || (NULL)
======================

对于2014-08-09和2014-08-16,计算天数是7

2 个答案:

答案 0 :(得分:0)

<?php

 $date1 = strtotime("2014-08-09");
 $date2 = strtotime("2010-08-16");
 $datediff = abs($date1 - $date2);
 echo floor($datediff/(60*60*24));

?>

绝对存在,所以你总能获得积极的结果。

Mysql 中使用:

SELECT DATEDIFF('new_date', 'old_date');

SELECT DATEDIFF('2014-08-16','2014-08-09');

答案 1 :(得分:0)

一种方法是让MySQL为结果集中的每一行计算一些数据,并将其作为结果的一部分包括在内,即使用用户定义的变量。

例如,(测试数据):

CREATE TABLE `demo` (
  `date` date NOT NULL
);

INSERT INTO `demo` (`date`) VALUES
('2014-09-09'),
('2014-09-10'),
('2014-09-12');

然后查询:

SET @date_last = '2014-08-01';
SELECT date, DATEDIFF(`date`, @date_last), (@date_last := `date`) FROM `demo`;

返回

|2014-09-09|39|2014-09-09
|2014-09-10| 1|2014-09-10
|2014-09-12| 2|2014-09-12