MySql Query - 日期范围内的日期范围 [英] MySql Query- Date Range within a Date Range

查看:144
本文介绍了MySql Query - 日期范围内的日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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 have products, that have a start date field and an end 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 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:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆