您现在的位置是:网站首页> 编程资料编程资料
Mysql 日期格式化及复杂日期区间查询_Mysql_
2023-05-27
480人已围观
简介 Mysql 日期格式化及复杂日期区间查询_Mysql_
前言
最近在做项目涉及到Mysql的复杂日期查询,日期查询其实在数据库中查询其实还是用的挺多的,比如查询开始日期到结束日期的区间信息,查询日期小于有效日期的信息,查询当天的日期,明天的日期,做比较等。
查询使用场景案例
时间区间查询
查询,2021年06月01号到2021年08月25号的数据
SELECT * FROM `dateTest` where DATE_FORMAT(date,'%Y%m%d') BETWEEN '20210601' and '20210825'
包括开始时间,不包括结束时间
但是DATE_FORMAT(date,'%Y%m')这种写法,无法使用索引,数据量大了以后查询超级慢
查询日期今天时间比较数据
select * from t_user t where t.CREATE_TIME>=curdate()
通过DATE把时间日期转换为时间格式
select * from t_user t where DATE (t.CREATE_TIME)>=DATE (now())
常用的周期时间查询
-- 今天 select fullName,addedTime from t_user where to_days(addedTime) <= to_days(now()); -- 昨天 select fullName,addedTime from t_user where to_days(NOW()) - TO_DAYS(addedTime) <= 1; -- 近7天 select fullName,addedTime from t_user where date_sub(CURDATE(),INTERVAL 7 DAY) <= DATE(addedTime); -- 近30天 SELECT fullName,addedTime FROM t_user where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(addedTime); -- 本月 SELECT fullName,addedTime FROM t_user WHERE DATE_FORMAT( addedTime, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' ); -- 上一月 SELECT fullName,addedTime FROM t_user WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( addedTime, '%Y%m' ) ) =1; -- 查询本季度数据 select fullName,addedTime FROM t_user where QUARTER(addedTime)=QUARTER(now()); -- 查询上季度数据 select fullName,addedTime FROM t_user where QUARTER(addedTime)=QUARTER(DATE_SUB(now(),interval 1 QUARTER)); -- 查询本年数据 select fullName,addedTime FROM t_user where YEAR(addedTime)=YEAR(NOW()); -- 查询上年数据 select fullName,addedTime FROM t_user where year(addedTime)=year(date_sub(now(),interval 1 year)); -- 查询距离当前现在6个月的数据 select fullName,addedTime FROM t_user where addedTime between date_sub(now(),interval 6 month) and now(); -- 查询当前这周的数据 SELECT fullName,addedTime FROM t_user WHERE YEARWEEK(date_format(addedTime,'%Y-%m-%d')) = YEARWEEK(now()); -- 查询上周的数据 SELECT fullName,addedTime FROM t_user WHERE YEARWEEK(date_format(addedTime,'%Y-%m-%d')) = YEARWEEK(now())-1; -- 查询上个月的数据 select fullName,addedTime FROM t_user where date_format(addedTime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m'); -- 查询当前月份的数据 select fullName,addedTime FROM t_user where DATE_FORMAT(addedTime,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m'); select fullName,addedTime FROM t_user where date_format(addedTime,'%Y-%m')=date_format(now(),'%Y-%m'); -- 查询指定时间段的数据 select fullName,addedTime FROM t_user where addedTime between '2017-1-1 00:00:00' and '2018-1-1 00:00:00'; select fullName,addedTime FROM t_user where addedTime >='2017-1-1 00:00:00' and addedTime < '2018-1-1 00:00:00';
mysql日期时间函数
1 得当前日期+时间(date + time)函数:now()
select now(); +---------------------+ | now() | +---------------------+ | 2021-08-31 16:16:32 | +---------------------+
2 获得当前日期+时间(date + time)函数:sysdate()
sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。看下面的例子就明白了:
select now(), sleep(3), now(); +---------------------+----------+---------------------+ | now() | sleep(3) | now() | +---------------------+----------+---------------------+ | 2021-08-31 16:20:12 | 0 | 2021-08-31 16:20:12 | +---------------------+----------+---------------------+
我们可以看到前后时间并没有改变,所以说是在执行开始之前值就得到了
sysdate() 日期时间函数,一般情况下很少用到。
3 获得当前时间戳函数:current_timestamp, current_timestamp()
select current_timestamp, current_timestamp(); +---------------------+---------------------+ | current_timestamp | current_timestamp() | +---------------------+---------------------+ | 2021-08-31 16:27:26 | 2021-08-31 16:27:26 | +---------------------+---------------------+
4 获取当前日期(date)函数: curdate()
-- 2021-08-31 select curdate();
mysql日期时间转换函数
1 日期时间转换字符串格式
Date/Time to Str(日期/时间转换为字符串)函数:date_format(date,format), time_format(time,format)
select date_format('2021-08-31 22:23:01', '%Y%m%d%H%i%s'); +----------------------------------------------------+ | date_format('2021-08-31 22:23:01', '%Y%m%d%H%i%s') | +----------------------------------------------------+ | 20210831222301 | +----------------------------------------------------+ MySQL 日期、时间转换函数:date_format(date,format), time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式。它是 str_to_date(str,format) 函数的 一个逆转换。
2 字符串转换为日期时间
MySQL Str to Date (字符串转换为日期)函数:str_to_date(str, format)
select str_to_date('08/31/2021', '%m/%d/%Y'); -- 2021-08-31 select str_to_date('08/31/2021' , '%m/%d/%y'); -- 2021-08-31 select str_to_date('08.31.2021', '%m.%d.%Y'); -- 2021-08-31 select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30 select str_to_date('08.09.2021 08:31:30', '%m.%d.%Y %h:%i:%s'); -- 2021-08-31 08:09:30 可以看到,str_to_date(str,format) 转换函数,可以把一些杂乱无章的字符串转换为日期格式。另外,它也可以转换为时间。“format” 可以参看 MySQL 手册。
3 (日期、天数)转换函数
MySQL (日期、天数)转换函数:to_days(date), from_days(days)
select to_days('0000-00-00'); -- 0 select to_days('2021-08-31'); -- 738398 4 (时间、秒)转换函数
MySQL (时间、秒)转换函数:time_to_sec(time), sec_to_time(seconds)
select time_to_sec('01:00:05'); -- 3605 select sec_to_time(3605); -- '01:00:05' 5 拼凑日期、时间函数
MySQL 拼凑日期、时间函数:makdedate(year,dayofyear), maketime(hour,minute,second)
select makedate(2021,31); -- '2021-01-31' select makedate(2021,32); -- '2021-02-01' select maketime(12,15,30); -- '12:15:30'
makedate(2021,31) 前面是年份,后面是天数,默认重1月份开始,如果后面天数是多少天,就进一个月数,
6 Unix 时间戳、日期 转换函数
unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)
select unix_timestamp(); -- 1218290027 select unix_timestamp('2008-08-08'); -- 1218124800 select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800 select from_unixtime(1218290027); -- '2008-08-09 21:53:47' select from_unixtime(1218124800); -- '2008-08-08 00:00:00' select from_unixtime(1218169800); -- '2008-08-08 12:30:00' select from_unixtime(1218169800, '%Y %D %M %h:%i:%s %x'); -- '2008 8th August 12:30:00 2008'mysql 日期时间计算函数
1 为日期增加一个时间间隔:date_add()
set @dt = now(); -- 定义变量 select date_add(@dt, interval 1 day); -- add 1 day select date_add(@dt, interval 1 hour); -- add 1 hour select date_add(@dt, interval 1 minute); -- ... select date_add(@dt, interval 1 second); select date_add(@dt, interval 1 microsecond); select date_add(@dt, interval 1 week); select date_add(@dt, interval 1 month); select date_add(@dt, interval 1 quarter); select date_add(@dt, interval 1 year); select date_add(@dt, interval -1 day); -- sub 1 day 加-1天也就是减1天
相关内容
- MySQL读取my.cnf的顺序问题详情_Mysql_
- MySQL 日期时间加减的示例代码_Mysql_
- MYSQL METADATA LOCK(MDL LOCK) 理论及加锁类型测试_Mysql_
- MySQL是怎么保证主备一致的_Mysql_
- Mysql ALTER TABLE加字段的时候到底锁不锁表_Mysql_
- 基于 Mysql 实现一个简易版搜索引擎_Mysql_
- MySQL的从库Seconds_Behind_Master延迟总结_Mysql_
- MySQL数据库 Load Data 多种用法_Mysql_
- MySQL数据库Shell import_table数据导入_Mysql_
- Mysql数据库的主从同步配置_Mysql_
点击排行
本栏推荐
