Sunday, April 17, 2005

MySQL get last 24 hours example SQL

select count(*) as cnt from log where date >= DATE_SUB(CURDATE(),INTERVAL 1 DAY);

6 comments:

Anonymous said...

It's good to know, that mysql don't use the query cache when U R executing query with time functions like now() or date_sub... so if is posible, it's better to send query with exact time informations.

lx said...

You should use NOW() instead of CURDATE() if you're pulling rows with a timestamp within the last 24 hours.

Read this article for details:
http://www.gizmola.com/blog/archives/51-Exploring-Mysql-CURDATE-and-NOW.-The-same-but-different..html

levani said...

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

Wicked Web Programmers said...

Good tips. I would also recommend using lowercase field names too with words separated by underscores.

Anonymous said...

i have a table order_master with a field of order_date(datetime) .

please tell me how to get last four hour order from order_master

Giusepep Urso said...

@anonymous
SELECT * FROM table_order
WHERE order_date > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 4 HOUR);

Ciao!