[英]MySQL Best Approach to get all days between two date values
I need to find all dates that are within two dates. 我需要查找两个日期内的所有日期。 This would normally be done with 通常这样做
BETWEEN 之间
, but I have a StartDate and en EndDate column. ,但我有一个StartDate和en EndDate列。 This means that the date actually stretches over some days. 这意味着日期实际上延伸了几天。
So a record could have values like this: 所以记录可能有这样的值:
id, status, startDate , endDate id,status, startDate , endDate
How can I find all rows, that are within those dates, provided I give the query a two dates. 如果我给查询两个日期,我怎样才能找到那些日期内的所有行。
To get records where the complete record period lies in the requested period: 要获取完整记录期间位于请求期间的记录:
SELECT *
FROM yourtable
WHERE startDate >= smallestDate AND
endDate <= largestDate;
To get records where the record period intersects the requested period: 获取记录期间与请求期间相交的记录:
SELECT *
FROM yourtable
WHERE startDate <= largestDate AND
endDate >= smallestDate;
And, for completeness, to get the records where the start of the records period lies in the requested period, but you don't care about the end of the record period: 并且,为了完整起见,要获取记录期间开始位于请求期间的记录,但您不关心记录期间的结束:
SELECT *
FROM yourtable
WHERE startDate BETWEEN smallestDate AND largestDate
And, vice versa, for the end date of the record period: 反之亦然,在记录期的结束日期:
SELECT *
FROM yourtable
WHERE endDate BETWEEN smallestDate AND largestDate
(All examples assume startDate
is always smaller than the endDate
, of course.) (当然,所有示例都假设startDate
总是小于endDate
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.