MySql 获取日期 本月、上月、本周、当天、最后一天

select curdate();                      --获取当前日期
select last_day(curdate());                   --获取当月最后一天。
select DATE_ADD(curdate(),interval -day(curdate())+1 day);   --获取本月第一天
select date_add(curdate()-day(curdate())+1,interval 1 month); -- 获取下个月的第一天
select DATEDIFF(date_add(curdate()-day(curdate())+1,interval 1 month ),DATE_ADD(curdate(),interval -day(curdate())+1 day)) from dual; --获取当前月的天数



select * from 表名 where to_days(时间字段) = to_days(now());

select * from 表名 where to_days(now())-to_days(时间字段) = 1;

select * from 表名 where date_sub(curdate(), interval 7 day) <= date(时间字段);

select * from 表名 where 时间字段 between date_sub(now(),interval 6 month) and now();

select * from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now());

select * from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now())-1;

select * from 表名 where date_format(时间字段,'%Y-%m')=date_format(now(),'%Y-%m');

select * from 表名 where date_format(时间字段,'%Y-%m')=date_format(date_sub(curdate(), interval 1 month),'%Y-%m');

posted @   Rukh  阅读(1954)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示