I have a mySQL table that includes the following three columns: IDENT, TIME, and USER. All are type varchar. For example:
IDENT = someuser::24::myisp.net::us
TIME = 2012-11-05 1:45:42
USER = someuser
What I’m trying to do is to count the number of distinct IDENTs in the past 5 days. I tried the following, but it fails (not all distinct IDENTs pulled):
SELECT DISTINCT(IDENT)
, ( UNIX_TIMESTAMP(now())
- UNIX_TIMESTAMP(STR_TO_DATE(TIME, '%Y-%m-%d %H:%i:%s'))
) / 86400 AS numdays
FROM `PS_LOGIN_LOG`
WHERE `USER` = 'someuser'
GROUP BY IDENT
HAVING `numdays` <= 5
It’s not entirely clear what you mean by “in the past 5 days“, but something along the following lines should do the trick: