选择今天日期在两个DATE列之间的MySQL行 [英] SELECT MySQL rows where today's date is between two DATE columns
问题描述
如何获取今天的日期在该行的两个DATE列之间(包括当日)的表中的行? 例如,使用表的这两列:
我如何在4月10日获得第一排和第二排,或者在25日获得第三排(包括我所说的)?
任何帮助将不胜感激.预先感谢!
您会发现很多人在使用between运算符,但我更喜欢使用简单的AND运算符.
之所以这样做,是因为尽管算子之间"是包含在内的,但简单的日期(2012-04-10)可以算作午夜,因此不会包含在内.
因此,这应该可以正常工作,并且始终包含日期范围的边界:
SELECT * FROM table WHERE from_date <= '2012-04-10' AND to_date >= '2012-04-10'
How can I get the rows in a table where today's date is between (inclusive) two DATE columns of that row? For example, take these two columns of a table:
How could I get the first and second rows on the 10th of April, or the 3rd row on the 25th (inclusive, like I said)?
Any help would be greatly appreciated. Thanks in advance!
You will find a lot of people using between operator, but I prefer using a simple AND operator.
I do that because although the between operator IS inclusive, simple dates (2012-04-10) can be counted as being midnight, and will thus not be inclusive.
So this should work just fine and will always include the boundaries of the date range:
SELECT * FROM table WHERE from_date <= '2012-04-10' AND to_date >= '2012-04-10'
这篇关于选择今天日期在两个DATE列之间的MySQL行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!