我想查询行以查看基于上一个季度月份的注册记录,例如:
当前月份/年份:2018年1月
查询应按注册月份显示所有记录,即:2017 年 10 月、2017 年 7 月、2017 年4 月等。
我使用以下查询,但只能选择过去 3 个月的行。
SELECT name, date, amount, agreement, bank
FROM `account`
WHERE (YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 3 MONTH) AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 3 MONTH))
你可以像这样做算术:
select a.*
from account a
where mod(month(date), 3) = mod(month(current_date), 3);
您似乎了解如何处理年份组件,但是像这样:
and date >= curdate() - interval 1 year
手动尝试这个
select *
from account
Where current_date >= dateadd(year, -1, Getdate())
And Month(current_date) In (
Month(Getdate()),
(Case When Month(Getdate())+3 > 12 Then Month(Getdate())+3-12 Else Month(Getdate())+3 End),
(Case When Month(Getdate())+6 > 12 Then Month(Getdate())+6-12 Else Month(Getdate())+6 End),
(Case When Month(Getdate())+9 > 12 Then Month(Getdate())+9-12 Else Month(Getdate())+9 End)
)