简体   繁体   中英

MySQL select datetime field where date equals today

I am trying to select a datetime field where the date equals today. However, the query never returns any rows.

I have this row in my database:

id    booked_at
1     2015-08-05 09:10:56

The query i'm using is:

select date(`booked_at`) from `booking_dates` where booked_at = CURDATE();

Why is this not returning the row above?

You have to strip the time part of booked_at because 2015-08-05 09:10:56 is not equal to 2015-08-05 . Try this:

select date(booked_at) from booking_dates where date(booked_at) = CURDATE();

To use index on column booked_at :

SELECT date(booked_at)
FROM booking_dates
WHERE booked_at >= CURDATE()
    AND booked_at < CURDATE() + INTERVAL 1 DAY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM