getaway
getaway

Reputation: 8990

querying results from mysql in the last 3 hours?

i want to query blog posts from the database created in the last 3 hours,

table

blogs{id,blog_text,date}

date format: datetime

Upvotes: 8

Views: 11749

Answers (2)

Alireza
Alireza

Reputation: 6848

Although @mways's answer is completely correct, it's more readable and less error prone to just use DATE_SUB as below:

select DATE_SUB(NOW(), INTERVAL 3 HOUR);

Upvotes: 4

mway
mway

Reputation: 4392

Try this:

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

Edit: my bad - replaced CURDATE() with NOW() as we're dealing with DateTimes.

Upvotes: 23

Related Questions