Answer the question
In order to leave comments, you need to log in
How to get data for last week, month, etc in mysql?
For example, for the last 7 days it is clear how
SELECT * FROM t WHERE date> NOW() - INTERVAL 7 DAY;
Answer the question
In order to leave comments, you need to log in
select id
from tab
where real_time > LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 2 MONTH))
AND real_time < DATE_ADD(LAST_DAY(CURDATE() - INTERVAL 1 MONTH), INTERVAL 1 DAY);
well, calculate the start and end dates of the last week or last month, and choose where date between date1 and date2
Hello.
This is how you can calculate the periods
per week:
SELECT CURDATE() - INTERVAL CASE WHEN DAYOFWEEK(CURDATE()) = 1 THEN 13 ELSE DAYOFWEEK(CURDATE()) + 5 END DAY -- start of the previous week
SELECT CURDATE() - INTERVAL CASE WHEN DAYOFWEEK(CURDATE()) = 1 THEN 6 ELSE DAYOFWEEK(CURDATE()) - 1 END DAY -- end of the previous week
SELECT DATE_ADD(CURDATE(), INTERVAL - DAYOFMONTH(CURDATE()) DAY) -- end of the previous month
SELECT DATE_ADD(DATE_ADD(CURDATE(), INTERVAL - DAYOFMONTH(CURDATE()) DAY), INTERVAL - DAYOFMONTH(DATE_ADD(CURDATE(), INTERVAL - DAYOFMONTH(CURDATE()) DAY)) + 1 DAY) -- start of the previous month
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question