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?
What about:
SELECT MAX(`value`) FROM myTable
WHERE `date` BETWEEN DATE_SUB(NOW(), INTERVAL 5 DAY) AND NOW()
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;
精彩评论