Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

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.

like image 799
Paraiba to Pusan Avatar asked Sep 26 '11 09:09

Paraiba to Pusan


People also ask

How do I get latest 10 records in MySQL?

SELECT * FROM ( SELECT * FROM yourTableName ORDER BY id DESC LIMIT 10 )Var1 ORDER BY id ASC; Let us now implement the above query. mysql> SELECT * FROM ( -> SELECT * FROM Last10RecordsDemo ORDER BY id DESC LIMIT 10 -> )Var1 -> -> ORDER BY id ASC; The following is the output that displays the last 10 records.

How do I get last 3 records in SQL?

SELECT * FROM (select * from suppliers ORDER BY supplier_name DESC) suppliers2 WHERE rownum <= 3 ORDER BY rownum DESC; Notice that although you want the last 3 records sorted by supplier_name in ascending order, you actually sort the supplier_name in descending order in this solution.

How do I select the last 5 records of a table?

1 Answer. ORDER BY id ASC; In the above query, we used subquery with the TOP clause that returns the table with the last 5 records sorted by ID in descending order. Again, we used to order by clause to sort the result-set of the subquery in ascending order by the ID column.

How do I select a specific record in MySQL?

MySQL SELECT statement is used to retrieve rows from one or more tables. The statement can also include UNION statements and subqueries. SELECT statement is used to fetch rows or records from one or more tables.


2 Answers

Use this sql query:

select * from myTable where Login_time > date_sub(now(), interval 3 minute) ; 
like image 176
DhruvPathak Avatar answered Sep 23 '22 09:09

DhruvPathak


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

select * from myTable where Login_time > select max(time) - interval 3 minute ; 
like image 24
sreewathsa k Avatar answered Sep 23 '22 09:09

sreewathsa k