简体   繁体   中英

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM