以下是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);Bagisto一个功能强大的电商平台系统,采用Laravel框架和Vue.js开发,支持前后端分离,适用于跨境电商、海外电商平台等行业。Bagisto的安装对系统和环境有特定的要求,具体如下:服务器:CPU:建议4核以上配置;内存:建议...
有同学咨询,他们公司的一台服务器,是一台Linux用作负载均衡的服务器,最近在使用过程中,随着连接数的增加,服务器突然挂掉了,这是怎么回事呢?今天丁老师就这个问题做一下解答。故障分析服务器:Linux用途:负载均衡服务故障情况:用户连接...
使用mysql在查询数据,涉及使用中文排序时,出现了排序结果不对的情况,如图:select * from area order by name asc;由上图可见,排序结果是乱的,并不是根据拼音ABCD按顺序排序的,引起这样的原因是为什...
有一个需求,需要提取PDF中表格的文本,项目是java环境,这个问题归属于"Java提取PDF文本"这一类。Java提取pdf文本的方法有很多,也有很多第三方库。丁老师使用的是Apache PDFBox,是Apach...
国外很多VPS购买后,只有密码,没有登录账号,如何登录呢?我们以one.com为例,购买了VPS后,只能设置密码,但是用root,也无法登录,是怎么回事呢?查了官方文档,原来修改的VPS密码,不是root的,这个VPS默认的账号是admin...
有同学咨询丁老师,他们公司的小程序和APP,是属于C端的客户服务类APP,提供会员注册、下单、发货、订单查询、业务办理进度查看等模块。其中有一个功能是在线咨询,之前是对接的人工在线客服坐席咨询,在用户量大的时候,峰值可能同时需要5-8个客服...
2026年6月8日,微信团队发布了《关于开发者接入微信AI生态的指引》文章,旨在面向开发者提供接入微信AI生态的能力,对于开发者和企业而言,把自己的微信小程序接入微信AI生态,将有机会被微信AI推荐和调用,在传统小程序运营的基础上,等于是又...
软件介绍Basgito是基于Laravel框架的免费电子商务平台,可以快速搭建自己企业专属的电子商务交易平台,适用于中小型外贸企业。该电商平台拥有无头laravel商务(headless laravel commerce)的强大功能,可以快...