开发者

mysql time comparison - strange result

I use a timestamp field that is set with current time when inserts.

It has the following entries.

    6   2011-02-10 09:36:31     174.129.228.67
    4   2011-02-10 09:36:36     174.129.228.67
    5   20开发者_高级运维11-02-10 09:36:41     174.129.228.67
    4   2011-02-10 12:11:08     192.168.0.4
    6   2011-02-10 12:48:21     192.168.0.22
    1   2011-02-10 12:48:45     192.168.0.22
    2   2011-02-10 12:49:00     192.168.0.22
    2   2011-02-10 12:49:01     66.220.158.251
    6   2011-02-10 12:49:18     66.220.158.251
    5   2011-02-10 13:00:25     66.220.149.249

Now() is

2011-02-10 17:20:04

When I execute, select * from list where timestampField > (now()-(8*60*60))

It return empty result set. Here, I expect entries entered after 2011-02-10 9:20:04.

When I execute, select * from list where timestampField > (now()-(14*60*60))

I get,

1   2011-02-10 12:48:45     192.168.0.22
2   2011-02-10 12:49:00     192.168.0.22
2   2011-02-10 12:49:01     66.220.158.251
6   2011-02-10 12:49:18     66.220.158.251

Why this. Could you tell me what is the proper way to get entries of previous hours.

Thanks.


You cannot subtract seconds from time in mysql. You need to use a function like this:

WHERE dateadded > DATE_SUB(NOW(), INTERVAL 8 HOUR)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜