又一个SQL查询

时间:2013-01-20 20:00:47

标签: mysql

我有一个mySQL表,其中包含以下三列:IDENT,TIME和USER。所有都是varchar类型。例如:

IDENT = someuser::24::myisp.net::us
TIME = 2012-11-05 1:45:42
USER = someuser

我要做的是计算过去5天内不同IDENT的数量。我尝试了以下方法,但它失败了(并非所有不同的IDENT都被拉出):

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

1 个答案:

答案 0 :(得分:1)

“过去5天内 ”的含义并不完全清楚,但以下几行应该可以解决问题:

SELECT COUNT(DISTINCT IDENT)
FROM   PS_LOGIN_LOG
WHERE  USER = 'someuser'
   AND TIME >= CURRENT_DATE - INTERVAL 5 DAY
   AND TIME <  CURRENT_DATE + INTERVAL 1 DAY