Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching rows added last hour

I keep a record of logins in a table. I have columns for id, ip, date and time. From that record of logins I wanna fetch logins made only in the last hour.

I'm sweeping through the MySQL docs on time and date functions, but I just can't seem to combine them correctly.

Can somebody help me?

like image 826
Michael Grons Avatar asked Sep 09 '10 23:09

Michael Grons


People also ask

How do I get last hour data in SQL?

Here is the SQL to show latest time using now() function. Here is the SQL to get last 1 hour data in MySQL. In the above query, we select only those rows whose order_date falls within past 1 hour interval. We use INTERVAL clause to easily substract 1 hour interval from present time obtained using now() function.

How can I get the last row inserted in SQL?

To get the last record, the following is the query. mysql> select *from getLastRecord ORDER BY id DESC LIMIT 1; The following is the output. The above output shows that we have fetched the last record, with Id 4 and Name Carol.

How do I fetch the last 10 rows?

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. We can match both records with the help of the SELECT statement.


1 Answers

Make use of the DATE_SUB() and Now() functions:

select count(*) as cnt from  log where date >= DATE_SUB(NOW(),INTERVAL 1 HOUR);  

Hope it helps you : )

like image 92
SDReyes Avatar answered Oct 12 '22 02:10

SDReyes