SQL can be written something like this:
select *
from d
where
(year = 2017) and (month <= 11)
or (year < 2017)
order by year desc, date desc
limit 4
It would be easier if the year + month was saved as a composite value - for example. the first month, but the same logic applies to searching for "n records before."
source
share