Hopefully, it\'s an easy solution, but I am trying to filter the data for the following:-
If you're selecting by date only, base your calculations on CURDATE
(which returns date only) rather than NOW
(which returns date and time). These examples will catch all times within the day ranges:
WHERE timestamp >= CURDATE()
WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND timestamp < CURDATE()
WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE())-1 DAY)
WHERE timestamp >= '2013-06-03' AND timestamp < '2013-06-08'
The "this week" depends on which day you start your week; I'll leave that to you. You can use the DAYOFWEEK function to tweak CURDATE()
to the proper ranges.
Addendum: OP's column type was INTEGER
, storing a UNIX timestamp, and my answer assumed the column type was TIMESTAMP
. Here's how to do all the same things with a UNIX timestamp value and still maintain optimization if the column is indexed (as the answers above will do if the TIMESTAMP
column is indexed)...
Basically, the solution is to just wrap the beginning and/or ending dates in the UNIX_TIMESTAMP function:
WHERE timestamp >= UNIX_TIMESTAMP(CURDATE())
WHERE timestamp >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 DAY)) AND timestamp < UNIX_TIMESTAMP(CURDATE())
WHERE timestamp >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE())-1 DAY))
WHERE timestamp >= UNIX_TIMESTAMP('2013-06-03') AND timestamp < UNIX_TIMESTAMP('2013-06-08')