请在MYSQL中帮助解决以下问题:
我有一个开始日期和结束日期。我需要计算当前月份的起始日期和结束日期之间的天数,直到当前日期为止。
示例1:
start_date = "2020-06-27"
end_date = "2020-09-27"
Since today's date is 2020-08-11 (11th Aug 2020). I need 11 as the output.
示例2:
start_date = "2020-08-08"
end_date = "2020-08-09"
I need 2 as the output.
Example3:
start_date = "2020-08-08"
end_date = "2020-08-14"
I need 4 as the output because current date is 11th Aug.
我尝试了datediff,period_diff,但还没有成功。 请在这里寻求帮助。
感谢您查看问题陈述。
答案 0 :(得分:2)
我想是这样的:
SELECT DATEDIFF(
LEAST(DATE_FORMAT(NOW(),"%Y-%m-%d"),"2020-08-14"),
GREATEST("2020-08-08",DATE_FORMAT(NOW(),"%Y-%m-01"))
)+1;
第二个问题的答案应该是2,
答案 1 :(得分:1)
答案 2 :(得分:0)
您可以结合使用subtract(15, 2, function (x) {
subtract(x, 2, function (x) {
console.log(x);
// x = 11
});
});
(类似
MONTH
功能
DATEDIFF