Fetching rows added last hour

Issue

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?

Solution

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 : )

Answered By – SDReyes

Answer Checked By – Mary Flores (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.