mysql:检查日期范围 [英] mysql : check date range

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

问题描述

我们具有类似

id | startDay | startMonth | startYear | endDay | endMonth | endYear    
1  | 22       | 12         | 2012      | 25     | 1        | 2013  

现在,我们还有另一个日期范围,例如startDate,startMonth,startYear,即 25 | 12 | 2012 和endDay,endMonth,endYear,即 10 | 1 | 2013 .

我希望查询从表中获取提供的日期范围在表中的记录.在这种情况下,意味着要先记录.
如何为此编写查询?

Now we have another date range like startDate,startMonth, startYear i.e. 25|12|2012 and endDay,endMonth,endYear i.e 10|1|2013.

I want the query to get the records from table where provided date range is present in table. Means record first in this case.
How to write query for this?

推荐答案

首先,使用@ MarcB 的建议.但是,如果您没有时间更改表,也可以使用以下查询.

First, take @MarcB's advice. But if you don't have time to alter your table, alternatively, you can also use the following query.

SELECT *
FROM tableName
WHERE ('2012-12-25' BETWEEN DATE(CONCAT_WS('-', startYear, startMonth, startDay)) AND
                  DATE(CONCAT_WS('-', endYear, endMonth, endDay))) AND
      ('2013-01-10' BETWEEN DATE(CONCAT_WS('-', startYear, startMonth, startDay)) AND
                  DATE(CONCAT_WS('-', endYear, endMonth, endDay))) 

  • SQLFiddle演示链接
    • SQLFiddle Demo Link
    • 这篇关于mysql:检查日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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