mysql日期获取

当前日期:select  curdate();

去年同期: select date_sub(curdate(), interval 1 year);

当前年份的第一天:select adddate(curdate(),-dayofyear(curdate())+1);

明年第一天:select  date_add(adddate(curdate(),-dayofyear(curdate())+1), interval 1 year);

本月第一天:select DATE_ADD(curdate(),interval -day(curdate())+1 day);


本月已过多少天:select  day(now());

本年已过多少天:select dayofyear(curdate());

查询当前时间到去年同期近一年的所有日期:

select a.checkDate

from (

select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) ) DAY as checkDate

from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a

cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b

cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c

cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as d

) a

where a.checkDate between curdate() - INTERVAL 1 YEAR and curdate()

你可能感兴趣的:(mysql日期获取)