Date Range Query MySQL

后端 未结 5 1490
予麋鹿
予麋鹿 2020-12-18 15:09

I need a query to select data between two dates with today\'s date as a reference.

The database has a datetime field for \"start\" and a datetime field for \"end\".<

5条回答
  •  被撕碎了的回忆
    2020-12-18 16:00

    You can use the mysql function curdate() to get the date as 2010-01-05, or now() to get the full datetime format 2010-01-05 12:55:09. It depends if you want to find items that start today or start right now.

    And assuming you don't have any entries that end before they begin you could rewrite the query as

    select * from news where start = curdate()
    

    It's probably a good idea to double-check your end dates too. And it seems that you're looking for a list of news articles that have already started but not yet ended, so the query looks like this

    select * from news where start < now() and end > now()
    

    You should even be able to do this, although I haven't tested it

    select * from news where now() between start and end
    

    Your mileage may vary.

    --Mark

提交回复
热议问题