I’m trying to retrieve entries in my DB using curdate() and I think there is some underlying logic that I’m unaware of. For instance:
SELECT * FROM foo WHERE event='bar'
AND created_at >= SUBDATE(CURDATE(), 90)
AND created_at <= CURDATE()
This call returns with the previous 90 days but it does not have any of the entries from today. As I understand it curdate() is only YYYY-MM-DD or YYYYMMDD and completely ignores the time of day, we save that for curtime() and now(). I suspect that time is being included somewhere in here because when I make a similar call in Rails and pass the date as a DateTime.beginning_of_day it works whereas any other way it will not include today up to a certain hour.
I’ve checked out a few sources, including the MySQL reference manual, and haven’t come up with any real answers. Is it possible that some element of the current time is being included in the query or is there some other business going on behind closed doors?
If your
created_atcolumn is a fulldatetimetype, then when comparing to a date, it will take the beginning of that date.So, comparing to
2011-01-01is akin to comparing to2011-01-01 00:00:00. That’s why you’re not getting anything back forCURDATE()(unless thecreated_attimestamp is exactly midnight on that date).Example: