Forum Moderators: open
what i am trying to do is see how many records were entered yesterday, not a total of the past two days, but just yesterday...
i know there are 34 records, but am getting "0" returned as the count....
i dont know if CURDATE is not calculating correctly against my "entry_time" date becuase of the hh:mm:ss...is that the case?
SELECT COUNT(*) FROM registration_table WHERE DATE_SUB(CURDATE(),INTERVAL 2 DAY) = entry_time AS yesterday
i dont know if CURDATE is not calculating correctly against my "entry_time" date becuase of the hh:mm:ss...is that the case?
I'd say yes. Are you sure the type of entry_time is DATE and not DATETIME?
Do a 'describe registration_table;' or 'show create table registration_table;' to make sure.
[dev.mysql.com ] gives some useful info including the following:
Prior to MySQL 5.0.42, when DATE values are compared with DATETIME values the time portion of the DATETIME value is ignored. Starting from MySQL 5.0.42, a DATE value is coerced to the DATETIME type by adding the time portion as '00:00:00'.
So if entry_time is of type DATETIME, then you should get it working by replacing entry_time in the where clause with cast(entry_time as date).
i have two other fields set to DATE, but "entry_time" is a DATETIME data format...
so my question now, is how can i calculate DATETIME against CURDATE and get the proper results?
SELECT COUNT(*) FROM registration_table WHERE DATE_SUB(CURDATE(),INTERVAL 2 DAY) = entry_time AS yesterday
you were absolutely freaking right....it is DATETIME....
so my question now, is how can i calculate DATETIME against CURDATE and get the proper results?
To repeat myself: replace entry_time in the where clause with cast(entry_time as date).
SELECT COUNT(*) FROM registration_table WHERE DATE_SUB(CURDATE(),INTERVAL 2 DAY) = cast(entry_time as date) And I'm not 100% certain, but I'm pretty sure ending a select statement with "AS alias" is invalid.
in any case...
again....you were absolutely freakin right...
CAST(entry_time AS DATE) works like a charm...
and i didnt end the query with that, because it looks like this:'
<snippet>
SUM(CASE WHEN DATE_SUB(CURDATE(),INTERVAL 6 DAY) = CAST(entry_time AS DATE) THEN 1 END) AS sevendaysback
</snippet>
dude, i freaking love this site forum way better than some other mysql forum that gives me nothing other an snide remarks...
U da man...
"you should read the manual"
well no crap...i did, and i still cant figure out why this wont work, and hence me asking in the forum....
some people live to be a$$holes on the forums....
in any case, thanks...
learned something new.