如何使用PHP从MySQL中选择最后3分钟的记录

时间:2011-09-26 09:49:50

标签: php mysql

这是我的SQL表:

+----------------+----------------+-----------+------------------------+ 
|   User_Name    |    Password    |    IP     |        Login_Time      |
+----------------+----------------+-----------+------------------------+
|  rthrthrhrt    |   fjdjetje5e   | 127.0.0.1 |   2011-09-24 18:02:06  |
|  Empty         |   Empty        | 127.0.0.1 |   2011-09-24 18:10:01  |
|  Empty         |   Empty        | 127.0.0.1 |   2011-09-24 18:04:00  |
|  rsyrt         |   rwytw4364    | 127.0.0.1 |   2011-09-24 18:08:59  |
|  eryrjrj5      |   Empty        | 127.0.0.1 |   2011-09-24 18:03:56  |
|  reutreuetry   |   reuretyre    | 127.0.0.1 |   2011-09-24 18:06:53  |
|  Empty         |   rthrtrt      | 127.0.0.1 |   2011-09-24 18:05:51  |
|  djdjgdjh      |   66735        | 127.0.0.1 |   2011-09-24 18:09:49  |
|  fgjdgjdhg     |   Empty        | 127.0.0.1 |   2011-09-24 18:07:46  |
|  Empty         |   Empty        | 127.0.0.1 |   2011-09-24 18:11:43  |
+----------------+----------------+-----------+------------------------+

我正在使用PHP和MySQL开发一个强力插件。我想选择最后3分钟的记录。

例如(我想做什么?):时间现在是:2011-09-26 9:45:00。我想选择2011-09-26 9:45:00和2011-09-26 9:42:00之间的所有记录。

2 个答案:

答案 0 :(得分:72)

使用此sql查询:

select * from myTable where Login_time > date_sub(now(), interval 3 minute) ;

答案 1 :(得分:8)

即使您的表格未更新,此查询仍然有效...

select * from myTable where Login_time > select max(time) - interval 3 minute ;