select *
from d
-- find all rows for this month or previous
where
(year = 2017) and (month <= 11) -- same month / earlier in same year
or (year < 2017) -- earlier year
-- for all found rows, order descending by year, then month
order by year desc, date desc
-- take the first 4 (descending by date)
limit 4
5条答案
按热度按时间vyu0f0g11#
或者
ej83mcc02#
sql可以这样编写:
如果将年+月存储为复合值(例如,月的第一个)会更简单,但同样的逻辑将应用于查找“n个记录”。
643ylb083#
检查一下这个
select * from mt_month where month_id<=11 order by month_id desc limit 4
9jyewag04#
您可以执行以下示例:
vxbzzdmp5#
既然你知道你想要的数据来自哪个月,为什么不用呢
select * from mt_month where start_date BETWEEN '2017-08-01' AND '2017-11-01'
返回数据?您可以传递查询的开始日期和结束日期