By querying mysql results for the last 3 hours?

I want to request blog entries from a database created in the last 3 hours,

Table

blogs{id,blog_text,date}

date format: datetime

+3
source share
2 answers

Try the following:

SELECT * FROM blogs WHERE date > DATE_ADD(NOW(), INTERVAL -3 HOUR)

Edit: my bad - replaced CURDATE()by NOW(), since we are dealing with DateTimes.

+10
source

Although @mways answer is completely correct, it is more readable and less error prone than just using DATE_SUB, as shown below:

select DATE_SUB(NOW(), INTERVAL 3 HOUR);
+1
source

Source: https://habr.com/ru/post/1772036/


All Articles