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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7553346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:56:30  来源:igfitidea点击:

How to select last 3 minutes' records from MySQL with PHP

phpmysql

提问by Paraiba to Pusan

this is my SQL table:

这是我的 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  |
+----------------+----------------+-----------+------------------------+

I am developing a brute force addon with PHP and MySQL. I would like to select last 3 minutes' records.

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

for example (what i'd like to do?): time is now: 2011-09-26 9:45:00. i would like to select all records between 2011-09-26 9:45:00 and 2011-09-26 9:42:00.

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

回答by DhruvPathak

Use this sql query:

使用这个 sql 查询:

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

回答by sreewathsa k

This query will work even if your table is not updated...

即使您的表未更新,此查询也将起作用...

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