[英]MySql Query- Date Range within a Date Range
I use mySql 5 and IIS.我使用 mySql 5 和 IIS。
I have products
, that have a start date
field and an end date
field.我有
products
,有一个start date
字段和一个end date
字段。
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.我需要运行一个查询,该查询将获取用户输入的开始日期和结束日期,以及 output 产品在日期范围内运行的天数。
Example:例子:
Offer1 - July 1 2011 thru July 31 2011
Query - July 1 2011 thru Sept 15 2011
Results = 31结果 = 31
Example:
Offer1 - July 1 2011 thru July 31 2011
Query - July 1 2011 thru July 15 2011
Results = 15结果 = 15
If your products
have a start_date
and an end_date
and your query has a qstart_date
and a qend_date
, then we want the number of days between:如果您的
products
有start_date
和end_date
并且您的查询有qstart_date
和qend_date
,那么我们需要以下天数:
GREATEST(start_date, qstart_date)
and和
LEAST(end_date,qend_date)
. . In MySQL I think this looks like
在 MySQL 我认为这看起来像
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".你会想忽略负数,用“0”代替它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.