In Ruby on Rails I'm doing something like:
Appointment.find( :first, :conditions => "staff_id = #{staff_id} AND datetimefield = #{datetime}")
... where datetimefield is of course, a datetime field. But, I o开发者_开发百科nly want rows where the date is equal to a given day, say 2/12/2011. I don't care about the time. What's an easy way to do this?
Thanks!
SELECT * FROM TheTable WHERE DATE(TheField) = '2010-02-12';
One possibility is to query for the value being between midnight and 23:59:59 on the target day, something like:
datetimefield = BETWEEN #{datetime.strftime('%Y-%m-%d 00:00:00')} AND #{datetime.strftime('%Y-%m-%d 23:59:59')}"
Another option (not recommended if your column is indexed, because you'll almost certainly invalidate the use of that index) could be something like this:
"strftime('%Y-%m-%d', datetimefield) = #{datetime.strftime('%Y-%m-%d')}"
精彩评论