MySql Query - 日期范围内的日期范围 [英] MySql Query- Date Range within a Date Range
问题描述
我使用mySql 5和IIS。
我有产品
,它们有一个开始日期
字段和结束日期
字段。
我需要运行一个用户输入的查询,结束日期,并输出产品在日期范围内运行的天数。
示例:
Offer1 - 2011年7月1日至2011年7月31日
查询 - 2011年7月1日至2011年9月15日
结果= 31
示例:
Offer1 - 2011年7月1日至7月31 2011
查询 - 2011年7月1日至2011年7月15日
结果= 15
如果您的产品
有一个 start_date
和 end_date
,您的查询具有 qstart_date
和 qend_date
,那么我们想要之间的天数:
GREATEST(start_date,qstart_date)
$ / pre>
和
LEAST(end_date,qend_date)
。在MySQL中,我觉得这样看起来像
1 + DATEDIFF('d',GREATEST(start_date,qstart_date),LEAST(end_date, qend_date))
而您将忽略负数,将其替换为0。 / p>
I use mySql 5 and IIS.
I haveproducts
, that have astart date
field and anend date
field.I need to run a query that will take user entered Start and End dates, and output the number of days that the product ran within the date range.
Example:
Offer1 - July 1 2011 thru July 31 2011 Query - July 1 2011 thru Sept 15 2011
Results = 31
Example: Offer1 - July 1 2011 thru July 31 2011 Query - July 1 2011 thru July 15 2011
Results = 15
解决方案If your
products
have astart_date
and anend_date
and your query has aqstart_date
and aqend_date
, then we want the number of days between:GREATEST(start_date, qstart_date)
and
LEAST(end_date,qend_date)
. In MySQL I think this looks like
1 + DATEDIFF ( 'd' , GREATEST(start_date, qstart_date) , LEAST(end_date,qend_date) )
And you'll want to ignore negative numbers, replacing them with "0".
这篇关于MySql Query - 日期范围内的日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!