以下是mysql常用的日期时间查询语句,其中,时间字段名是默认以datetime格式的,如果是int时间戳格式的时间,则需要在时间字段名字段上,加上from_unixtime(时间字段名)。
查询当天的数据:
#时间字段为y-m-d格式,如果时间字段为int类型的时间戳,则使用from_unixtime(时间字段名)
select * from dls6 where to_days(时间字段名)=to_days(now());
查询昨天的数据:
select * from dls6 WHERE to_days(now())-to_days(时间字段名)<=1;
查询本周的数据:
select * from dls6 WHERE yearweek(date_format(时间字段名,'%Y-%m-%d'))=yearweek(now());
查询最近一周(近7天)的数据:
select * from dls6 where date_sub(curdate(),interval day)<=date(时间字段名);
查询本月的数据:
select * from dls6 where date_sub(时间字段名,'%Y%m')=date_format(curdate(),'%Y%m');
查询最近一月(近30天)的数据:
select * from dls6 where date_sub(curdate(),interval 30 day)<=date(时间字段名);
查询最近6个月(以当前时间计算)的数据:
select * from dls6 where 时间字段名 between date_sub(now(),interval 6 month) and now();
查询上月的数据:
select * from dls6 where period_diff(date_format(now(),'%Y%m'),date_format(时间字段名,'%Y%m'))=1;
查询本季度的数据:
select * from dls6 where quarter(时间字段名)=quarter(now());
查询上季度数据:
select * from dls6 where quarter(时间字段名)=quarter(date_sub(now(),interval 1 quarter));
查询本年数据:
select * from dls6 where year(时间字段名)=year(now());
查询上年数据:
select * from dls6 where year(时间字段名)=year(date_sub(now(),interval 1 year));
查询指定时间段的数据:
select * from dls6 where 时间字段名>='2025-01-01 00:00:00' and 时间字段名<='2025-12-31 23:59:59';计算两个日期的时间差:
相差秒:
select timestampdiff(second,时间字段1,时间字段2);
相差分钟数:
select timestampdiff(minute,时间字段1,时间字段2);
相差小时数:
select timestampdiff(hour,时间字段1,时间字段2);
相差天数:
select timestampdiff(day,时间字段1,时间字段2);
相差周数:
select timestampdiff(week,时间字段1,时间字段2);
相差月数:
select timestampdiff(month,时间字段1,时间字段2);
相差季度数:
select timestampdiff(quarter,时间字段1,时间字段2);
相差年数:
select timestampdiff(year,时间字段1,时间字段2);
获取本周第一天的日期:
select date_sub(curdate(),interval weekday(curdate())+1 day);
获取本周最后一天的日期:
select date_sub(curdate(),interval weekday(curdate())-5 day);
获取上周的第一天日期:
select date_sub(curdate(),interval weekday(curdate())+8 day);
获取上周的最后一天日期:
select date_sub(curdate(),interval weekday(curdate())+2 day);
获取本月最后一天的日期:
select last_day(now());
获取上月最后一天的日期:
select last_day(now()-interval 1 month);
获取本季度最后一天的日期:
select last_day(makedate(extract(year from curdate()),1) + interval quarter(curdate())*3-1 month);
获取上季度最后一天的日期:
select last_day(makedate(extract(year from curdate()),1) + interval quarter(curdate())*3-4 month);在开发苹果IOS APP时,遇到了个问题。最近把我的mac os更新到了Sequoia版本,然后xcode15居然不能用了(囧),查了很多资料,发现原来是Sequoia不支持xcode15,这可怎么办呢,没必要因为这个重装系统啊,在经过...
挺不错的浏览器,强加入了edge image viewer图片浏览器,感觉不是那么好,对于开发者来说,更想要原生的东西。包括微信网页版里的表情图片,用edge image viewer打开的话,无法保存为gif格式,即使保存了,也只是第...
用MCP+知识库+工作流+OpenClaw实现自动化办公,是以OpenClaw为核心,对接AI大模型完成日常办公自动化,可以实现企业级智能化办公,可将复杂任务自动化、降低开发成本的AI解决方案。本文将从技术原理、组件介绍、协同方式、应用...
有同学咨询,在宝塔面板部署的网站,打开后报错,提示Warning:is_dir():open_basedir restriction in effect. File is not within the allowed path,这是怎么...
上一期我们讲到了《微信小程序发布后多久可以能被搜索到?》,我们已经知道了微信小程序在上架后,多久能被搜索到的一个时间范围。本期我们讲一下,微信小程序被搜索,如何提升排名,是搜索结果靠前的方法。首先,微信小程序的搜索结果排名,并没有一个固定的...
2026年6月8日,微信团队发布了《关于开发者接入微信AI生态的指引》文章,旨在面向开发者提供接入微信AI生态的能力,对于开发者和企业而言,把自己的微信小程序接入微信AI生态,将有机会被微信AI推荐和调用,在传统小程序运营的基础上,等于是又...
在使用wordpress的过程中,有的时候部分页面打不开,或是程序不能正常使用,也没有显示具体的错误信息,这个时候该怎么办呢?首先是查看错误日志,可以查看wordpress本身的错误日志,以及php的错误日志。这时有朋友提问了,wordpr...
开发抖音小程序所需资料及费用概览随着短视频平台抖音(TikTok)在全球范围内的迅速崛起,越来越多的企业和个人开始意识到在抖音平台上开发小程序的重要性。抖音小程序不仅可以增强品牌曝光率,还能为用户提供更加便捷的服务体验。那么问题来了,开发一...