MYSQL select where the date is on this day
To get records for the current day, it should be enough:
SELECT COUNT(entryID)
FROM table
WHERE date >= CURDATE()
As Michael points out in the comments, he reviews all entries from the last two days in his current form.
The operator >=
is only needed if, date
in fact, a datetime
- if it is just a type date
, =
should suffice.
source to share
CURDATE () returns a date like '2012-03-30', not a timestamp like '2012-03-30 21:38:17'. Subtracting one day also only returns the date, not the timestamp. If you want to think of a date as a timestamp, think of it as the beginning of that day, that is, "00: 00: 00" time.
And that's why, why this
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 1 DAY)
and this one
WHERE date > CURDATE()
do the same thing.
I have another hint: SELECT COUNT(entryID)
u SELECT COUNT(*)
give the same result. SELECT COUNT(*)
gives the database machine more room to optimize the count, so it is COUNT(*)
often (not always) faster than COUNT(field)
.
source to share