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

this is my SQL table:

+----------------+----------------+-----------+------------------------+ 
|   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.

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use this sql query:

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

Method 2

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

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x