0

我有一个相当简单的表,其中字段 lastloginon 是时间戳,其中存储的日期如下所示:2013-03-17 17:12:58

如何选择过去 20 分钟内的所有记录?

我正在尝试使用以下代码:

 SELECT * FROM who_is_online WHERE lastloginon > (now() - interval 20 minute)

这会产生错误的结果集,即 qyery 在 15:30 执行它会显示 4 条记录,这些记录插入到:

2013-03-18 13:13:36
2013-03-18 13:35:12
2013-03-18 14:43:42
2013-03-18 14:55:34

有人知道我在做什么错吗?

这是表导出,只是希望有人能发现我做错了什么:

CREATE TABLE IF NOT EXISTS `who_is_online` (
  `recid` int(11) NOT NULL auto_increment,
  `username` varchar(255) default NULL,
  `lastloginon` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `memberip` varchar(255) default NULL,
  `sessionid` varchar(255) NOT NULL,
  `active` tinyint(4) default NULL,
  PRIMARY KEY  (`recid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=17 ;

--
-- Dumping data for table `who_is_online`
--

INSERT INTO `who_is_online` (`recid`, `username`, `lastloginon`, `memberip`, `sessionid`, `active`) VALUES
(7, ' admin', '2013-03-17 14:43:43', '87.202.163.222', '7kve2idmdbd47aksab4j9hqg74', 1),
(8, ' mmitechnic', '2013-03-17 15:16:39', '5.54.84.144', 'mofgpldp2lu30enhitvak9m4t3', 1),
(9, ' theodor71', '2013-03-17 17:12:58', '85.75.243.246', '4enpk49oi7cg0blumgsd0lu0m7', 1),
(10, ' xElfiex', '2013-03-18 12:38:05', '5.55.27.203', 'tr003d6qbd71v2i5grnuako362', 1),
(11, ' admin', '2013-03-18 12:56:06', '85.74.166.110', '7kve2idmdbd47aksab4j9hqg74', 1),
(12, ' xElfiex', '2013-03-18 13:13:36', '5.55.27.203', 'tr003d6qbd71v2i5grnuako362', 1),
(13, ' admin', '2013-03-18 13:35:12', '85.74.166.110', '7kve2idmdbd47aksab4j9hqg74', 1),
(14, ' admin', '2013-03-18 14:43:42', '85.74.166.110', '7kve2idmdbd47aksab4j9hqg74', 1),
(15, ' admin', '2013-03-18 14:55:34', '85.74.166.110', '7kve2idmdbd47aksab4j9hqg74', 1),
(16, ' admin', '2013-03-18 15:32:27', '85.74.166.110', '7kve2idmdbd47aksab4j9hqg74', 1);

问候,佐兰

4

3 回答 3

4

尝试date_sub(now(), interval 20 minute)代替(now() - interval 20 minute)

于 2013-03-18T13:39:38.213 回答
0

It looks to me like you may have a timezone issue. It's hard to tell without knowing more about your server and client configuration.

But you might try this WHERE clause ...

   lastloginon >= UNIX_TIMESTAMP() - (20  * 60)

This will compare your column numerically to the unix timestamp of twenty minutes ago.

于 2013-03-18T14:26:20.070 回答
0

mysql 服务器上的时间与您可能期望的不同。为了在服务器上查找时间,请尝试使用此查询:

select now();

现在,您的查询是正确的,但您对表的插入不正确。而不是使用

$lastlogin = date('Y-m-d H:i:s', strtotime("$lastlogin + 2 hours"));

在网站方面,将此字段留空并使用您的"default CURRENT_TIMESTAMP".

另一方面,请注意 username 有一个无法解释的前导空格。

于 2013-03-18T15:22:40.523 回答