选择MySQL行,其中今天的日期在两个DATE列之间

时间:2022-09-18 12:56:46

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列之间(包括)?例如,取一个表的这两列:

选择MySQL行,其中今天的日期在两个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日的第3行(包括,就像我说的那样)?

Any help would be greatly appreciated. Thanks in advance!

任何帮助将不胜感激。提前致谢!

3 个解决方案

#1


14  

You will find a lot of people using between operator, but I prefer using a simple AND operator.

你会发现很多人在运营商之间使用,但我更喜欢使用一个简单的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'

#2


16  

You can add a condition as follows

您可以按如下方式添加条件

DATE(NOW()) between date1 and date2

#3


6  

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

#1


14  

You will find a lot of people using between operator, but I prefer using a simple AND operator.

你会发现很多人在运营商之间使用,但我更喜欢使用一个简单的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'

#2


16  

You can add a condition as follows

您可以按如下方式添加条件

DATE(NOW()) between date1 and date2

#3


6  

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