简体   繁体   English

MySQL日期之间的日期

[英]MySQL Dates between Dates

I am wondering if there is a way, in MySQL 4.1+, to query the specific dates between date ranges in my records? 我想知道在MySQL 4.1+中是否有办法查询记录中日期范围之间的具体日期?

For a quick example, I have two records with datetime ranges, like 举个简单的例子,我有两个日期时间范围的记录,比如

ID  |  Activity  |  Start Date         | End Date

1      Closed       1/1/11 11:00:00      1/4/11 11:00:00

2      Open         1/15/11 10:00:00     1/19/11 09:00:00

What I want to know is, is there a way I can get the dates between the "Start Date" and "End Date" for each of these records? 我想知道的是,有没有办法可以获得每个记录的“开始日期”和“结束日期”之间的日期? Like so: 像这样:

1/2/11, 1/3/11 1/2 / 2,1 / 3/11

And

1/16/11, 1/17/11, 1/18/11 1/16/11,1 / 17 / 11,1 / 18/11

Or at least a way to get close to that and use PHP to finish the rest of the way? 或者至少是一种接近它并使用PHP完成其余部分的方法? Thanks! 谢谢!

这可能是你唯一的问题:

SELECT * FROM `your_table` WHERE start_date > '2011-01-01' AND end_date < '2011-01-04'

Jordan, 约旦,

This is not directly possible with MySQL 4. And i think this logic should not be put in your database layer. 这不是MySQL 4直接可行的。我认为这个逻辑不应该放在你的数据库层。

There is however a question on StackOverflow that relates to your problem and the solution is a stored procedure (but for that you need mysql 5). 然而,StackOverflow上有一个与您的问题有关的问题,解决方案是一个存储过程(但为此您需要mysql 5)。 View it here: Get a list of dates between two dates 在此处查看: 获取两个日期之间的日期列表

I found a not ready to use, but sensible approach to your problem in PHP at this web-page: http://prajapatinilesh.wordpress.com/2009/05/07/get-all-dates-between-two-dates-using-php-code/ 我在这个网页上发现了一个不准备使用,但在PHP中解决问题的合理方法: http//prajapatinilesh.wordpress.com/2009/05/07/get-all-dates-between-two-dates-使用-PHP-代码/

Hope this helps you in the right direction. 希望这可以帮助您朝着正确的方向前进。

我之前在我自己的mysql查询中对此表示赞同。

SELECT id FROM table WHERE some_date BETWEEN start_date AND end_date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM