Timothée HENRY
Timothée HENRY

Reputation: 14604

MySQL query for maximum value of all records from last 5 days

I have a MySQL table:

myTable {Int id, Int value, Date date}

I wish to find the maximum value of all records from last 5 days.

So far I only managed to either get the maximum of all records for this day:

SELECT max(`value`) FROM myTable where `date` = CURDATE()

or all records from the last 5 days:

SELECT * FROM myTable WHERE `date` BETWEEN CURDATE() - INTERVAL 5 DAY AND CURDATE()

How do I get the maximum value of all records from last 5 days?

Upvotes: 0

Views: 2916

Answers (2)

grantk
grantk

Reputation: 4058

You could create a temporary table

create temporary table temp_table SELECT * FROM myTable WHERE `date` BETWEEN CURDATE() - INTERVAL 5 DAY AND CURDATE()

Then

select max('value') from temp_table;

Upvotes: 0

Marco
Marco

Reputation: 57583

What about:

SELECT MAX(`value`) FROM myTable 
WHERE `date` BETWEEN DATE_SUB(NOW(), INTERVAL 5 DAY) AND NOW()

Upvotes: 5

Related Questions