You need to select entries with date which is less than the current date for 7 days and not more than 30 days.

Example: (Imagine that today 2018-11-28)

id | Дата 1 | 2018-12-28 2 | 2018-12-22 3 | 2018-10-27 

It should display only the second entry. (2018-12-21) because It falls into this gap. My unsuccessful attempt:

 SELECT `user_id` FROM users WHERE user_last_activity <= CURDATE()-6 AND user_last_activity > CURDATE()-30 
  • field type user_last_activity ? - Igor

2 answers 2

The numerical value YYYYMMDD , returned by the CURDATE function, will not give the correct result for arithmetic operations that cross the month boundaries.

 SELECT `user_id` FROM users WHERE user_last_activity <= DATE_ADD(CURDATE(), INTERVAL -6 DAY) AND user_last_activity > DATE_ADD(CURDATE(), INTERVAL -30 DAY) 
     SELECT `user_id` FROM users WHERE user_last_activity <= CURRENT_DATE - INTERVAL 6 DAY AND user_last_activity >= CURRENT_DATE - INTERVAL 30 DAY