I have saved the dates of a user’s registration as a datetime, so that’s for instance 2011-12-06 10:45:36. I have run this query and I expected this item – 2011-12-06 10:45:36 – will be selected:
SELECT `users`.* FROM `users` WHERE created_at >= '2011-12-01' AND
created_at <= '2011-12-06'
But is not. Exist any elegant way, how to select this item? As a first idea that I got was like 2011-12-06 + 1, but this doesn’t looks very nice.
Your problem is that the short version of dates uses midnight as the default. So your query is actually:
This is why you aren’t seeing the record for 10:45.
Change it to:
You can also use:
Which will select all users in the same interval you are looking for.
You might also find the BETWEEN operator more readable: