Comments - Select data on previous month
Content reproduced on this site is the property of its respective owners,
and this content is not reviewed in advance by MariaDB. The views, information and opinions
expressed by this content do not necessarily represent those of MariaDB or any other party.
CURDATE()
DATE_ADD()
DATE_SUB()
DATEDIFF()
Generate data:
First create a table with records in interval of "n" months:
In my case it is 3 months starting from now()
Results:
Insert data into the table
DELIMITER // WHILE (@counter < @diff) DO INSERT daterange VALUES (DATE_ADD(@from, INTERVAL @counter:=@counter + 1 DAY)); END WHILE; // DELIMITER ;Results:
Get the first and last 5 days:
Get the first 5 days:
Get the last 5 days from table:
Get the interval
Get the x [day,month] data (@x) from table,
starting from @mydate, where @mydate is 1 day before today
Result for day interval:
Result for month interval: