比如今年当前月份是3月份,
我现在要查询去年4月份的数据,应该怎么写?
1.程序处理一下,获取到去年同期下一个月的日期,然后当做参数传入SQL语句中执行。这个就不写代码了
2.使用mysql的日期函数操作 date_sub
select date_sub(now(), interval 1 year); -- 去年同期日期
select date_sub(now(), interval 12 month); -- 去年同期日期
select date_sub(now(), interval 11 month); -- 去年同期下月日期
select date_format(date_sub(now(), interval 11 month), '%Y%m'); -- 格式化一下
-- 输出结果
-- 2018-03-12 16:57:34
-- 2018-03-12 16:57:34
-- 2018-04-12 16:57:34
-- 201804
select * from ht_invoice_information
where year(create_date)=year(date_sub(now(),interval 1 year));