Selecting entries by date - >= NOW(), MySQL

前端 未结 6 1079
盖世英雄少女心
盖世英雄少女心 2020-12-13 14:11

I have a Calendar of Events table and I would like to select events with dates equal to or greater than today. When I use the following SELECT statement, it only retrieves e

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 14:48

    The reason that this query:

    SELECT * FROM events WHERE event_date >= NOW()
    

    ...returns records from the future, is because NOW() includes the time as well as the date. And that time portion indicates the time at which the [function or triggering] statement began to execute. So that means the start of the day in a DATETIME data type looks like: 2010-06-24 00:00:00 (YYYY-MM-DD HH:MM:SS, to microsecond precision), which NOW() would show something like 2010-06-24 14:24:31...

    Here are your options:

    SELECT * FROM events WHERE event_date >= DATE(NOW())
    SELECT * FROM events WHERE event_date >= DATE(CURRENT_TIMESTAMP)
    SELECT * FROM events WHERE event_date >= CURRENT_DATE()
    SELECT * FROM events WHERE event_date >= CURRDATE()
    

提交回复
热议问题