SELECT MySQL rows where today's date is between two DATE columns

前端 未结 4 1179
粉色の甜心
粉色の甜心 2021-02-05 20:27

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:

相关标签:
4条回答
  • 2021-02-05 20:39

    You can add a condition as follows

    DATE(NOW()) between date1 and date2
    
    0 讨论(0)
  • 2021-02-05 20:42

    If you have date (not datetime) columns, use CURTIME() or DATE(NOW()), never NOW() as CesarC correct wrote and you can use BETWEEN.

    SELECT * FROM table WHERE CURTIME() BETWEEN from_date AND to_date

    0 讨论(0)
  • 2021-02-05 20:55

    Just use the SQL now() function to compare the date columns like so:

    SELECT * from table where now() >= from_date and now() <= to_date
    
    0 讨论(0)
  • 2021-02-05 20:57

    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'
    
    0 讨论(0)
提交回复
热议问题