SELECT MySQL 行,其中今天的日期位于两个 DATE 列之间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10202427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SELECT MySQL rows where today's date is between two DATE columns
提问by JJJollyjim
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:
如何获取表中的行,其中今天的日期介于(包括)该行的两个 DATE 列之间?例如,以表的这两列为例:
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)?
我怎样才能在 4 月 10 日获得第一行和第二行,或者在 25 日获得第三行(包括我所说的)?
Any help would be greatly appreciated. Thanks in advance!
任何帮助将不胜感激。提前致谢!
回答by Chetter Hummin
You can add a condition as follows
您可以按如下方式添加条件
DATE(NOW()) between date1 and date2
回答by Bill
You will find a lot of people using between operator, but I prefer using a simple AND operator.
您会发现很多人使用 between 运算符,但我更喜欢使用简单的 AND 运算符。
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.
我这样做是因为虽然操作符之间是包容性的,但简单的日期 (2012-04-10) 可以算作午夜,因此不包括在内。
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'
回答by Eric Sites
Just use the SQL now() function to compare the date columns like so:
只需使用 SQL now() 函数来比较日期列,如下所示:
SELECT * from table where now() >= from_date and now() <= to_date