user968880
user968880

Reputation: 645

How to filter data in mySQL query based on timestamp?

I want to fetch data of last 1 hour from mySQL database. I can certainly do it in java after results have been fetched but i don't want to unnecessarily put load on application. Please help.

Upvotes: 1

Views: 1834

Answers (3)

Krishna Rani Sahoo
Krishna Rani Sahoo

Reputation: 1539

select * from yourtable where created_at between NOW()  and date_sub(NOW() , interval 1 HOUR) 

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

If the column is of type DATE you can use the DATE_SUB function, which substracts a given time interval from a DATE.

SELECT * FROM [table] WHERE [date_column] > DATE_SUB(NOW(), INTERVAL 1 HOUR)

Documentation

Upvotes: 0

jaczes
jaczes

Reputation: 1394

select * from tb_data
where createdtime > ADDDATE(NOW(), INTERVAL -1 HOUR)

Upvotes: 1

Related Questions