我有两列:rental_date
和actual_retdate
。我需要找到actual_retdate
和rental_date
之间的天数。对于某些情况,actual_retdate
可能为空,因此我希望在这些情况下找到今天的日期和租赁日期之间的天数。在两个日期之间使用空值查找SQL Days
目前我有:
select rental_date, actual_retdate, actual_retdate - rental_date as 'Daysbetween'
from rental_agreement
这给我的答案:
Rental_date actual_retdate Daysbetween 2014-07-04 2014-07-11 7 2016-05-06 2016-05-08 2 2016-08-07 2016-09-07 100 2015-02-02 2015-02-10 8 2015-10-10 2015-10-15 5 2015-08-07 2015-08-17 10 2017-02-04 NULL NULL 2016-07-08 2016-07-16 8 2017-03-02 NULL NULL 2015-03-15 2015-04-15 100
在MySQL中,减法运算符不保证在两个日期值之间返回一个数字天。它在某些情况下可能有效(当两个日期在相同的年份和月份),但在更一般的情况下不会返回正确的值。 (请注意返回'100'的行。在MySQL中,我们可以使用'TIMESTAMPDIFF(DAY,dateexpr1,dateexpr2)'来获得天数的差异。 – spencer7593