前言
最近在做项目涉及到Mysql的复杂日期查询,日期查询其实在数据库中查询其实还是用的挺多的,比如查询开始日期到结束日期的区间信息,查询日期小于有效日期的信息,查询当天的日期,明天的日期,做比较等。
查询使用场景案例
- 时间区间查询
查询,2021年06月01号到2021年08月25号的数据
1SELECT 2 * 3FROM 4 `dateTest` 5where DATE_FORMAT(date,'%Y%m%d') BETWEEN '20210601' and '20210825'
包括开始时间,不包括结束时间
但是DATE_FORMAT(date,'%Y%m')这种写法,无法使用索引,数据量大了以后查询超级慢
- 查询日期今天时间比较数据
1select * from t_user t where t.CREATE_TIME>=curdate()
通过DATE把时间日期转换为时间格式
1select * from t_user t where DATE (t.CREATE_TIME)>=DATE (now())
- 常用的周期时间查询
1-- 今天 2select fullName,addedTime from t_user where to_days(addedTime) <= to_days(now()); 3-- 昨天 4select fullName,addedTime from t_user where to_days(NOW()) - TO_DAYS(addedTime) <= 1; 5-- 近7天 6select fullName,addedTime from t_user where date_sub(CURDATE(),INTERVAL 7 DAY) <= DATE(addedTime); 7-- 近30天 8SELECT fullName,addedTime FROM t_user where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(addedTime); 9-- 本月 10SELECT fullName,addedTime FROM t_user WHERE DATE_FORMAT( addedTime, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' ); 11-- 上一月 12SELECT fullName,addedTime FROM t_user WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( addedTime, '%Y%m' ) ) =1; 13-- 查询本季度数据 14select fullName,addedTime FROM t_user where QUARTER(addedTime)=QUARTER(now()); 15-- 查询上季度数据 16select fullName,addedTime FROM t_user where QUARTER(addedTime)=QUARTER(DATE_SUB(now(),interval 1 QUARTER)); 17-- 查询本年数据 18select fullName,addedTime FROM t_user where YEAR(addedTime)=YEAR(NOW()); 19-- 查询上年数据 20select fullName,addedTime FROM t_user where year(addedTime)=year(date_sub(now(),interval 1 year)); 21-- 查询距离当前现在6个月的数据 22select fullName,addedTime FROM t_user where addedTime between date_sub(now(),interval 6 month) and now(); 23 24-- 查询当前这周的数据 25SELECT fullName,addedTime FROM t_user WHERE YEARWEEK(date_format(addedTime,'%Y-%m-%d')) = YEARWEEK(now()); 26-- 查询上周的数据 27SELECT fullName,addedTime FROM t_user WHERE YEARWEEK(date_format(addedTime,'%Y-%m-%d')) = YEARWEEK(now())-1; 28-- 查询上个月的数据 29select fullName,addedTime FROM t_user where date_format(addedTime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m'); 30-- 查询当前月份的数据 31select fullName,addedTime FROM t_user where DATE_FORMAT(addedTime,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m'); 32select fullName,addedTime FROM t_user where date_format(addedTime,'%Y-%m')=date_format(now(),'%Y-%m'); 33 34-- 查询指定时间段的数据 35select fullName,addedTime FROM t_user where addedTime between '2017-1-1 00:00:00' and '2018-1-1 00:00:00'; 36select fullName,addedTime FROM t_user where addedTime >='2017-1-1 00:00:00' and addedTime < '2018-1-1 00:00:00';
mysql日期时间函数
- 得当前日期+时间(date + time)函数:
now()
1select now(); 2 3+---------------------+ 4| now() | 5+---------------------+ 6| 2021-08-31 16:16:32 | 7+---------------------+
- 获得当前日期+时间(date + time)函数:
sysdate()
sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。看下面的例子就明白了:
1select now(), sleep(3), now(); 2+---------------------+----------+---------------------+ 3| now() | sleep(3) | now() | 4+---------------------+----------+---------------------+ 5| 2021-08-31 16:20:12 | 0 | 2021-08-31 16:20:12 | 6+---------------------+----------+---------------------+
我们可以看到前后时间并没有改变,所以说是在执行开始之前值就得到了
sysdate() 日期时间函数,一般情况下很少用到。
- 获得当前时间戳函数:
current_timestamp, current_timestamp()
1select current_timestamp, current_timestamp(); 2 3+---------------------+---------------------+ 4| current_timestamp | current_timestamp() | 5+---------------------+---------------------+ 6| 2021-08-31 16:27:26 | 2021-08-31 16:27:26 | 7+---------------------+---------------------+
4 获取当前日期(date)函数: curdate()
1-- 2021-08-31 2select curdate();
mysql日期时间转换函数
- 日期时间转换字符串格式
Date/Time to Str(日期/时间转换为字符串)函数:date_format(date,format), time_format(time,format)
1select date_format('2021-08-31 22:23:01', '%Y%m%d%H%i%s'); 2+----------------------------------------------------+ 3| date_format('2021-08-31 22:23:01', '%Y%m%d%H%i%s') | 4+----------------------------------------------------+ 5| 20210831222301 | 6+----------------------------------------------------+
MySQL 日期、时间转换函数:date_format(date,format), time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式。它是 str_to_date(str,format) 函数的 一个逆转换。
- 字符串转换为日期时间
MySQL Str to Date (字符串转换为日期)函数:str_to_date(str, format)
1select str_to_date('08/31/2021', '%m/%d/%Y'); -- 2021-08-31 2select str_to_date('08/31/2021' , '%m/%d/%y'); -- 2021-08-31 3select str_to_date('08.31.2021', '%m.%d.%Y'); -- 2021-08-31 4select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30 5select str_to_date('08.09.2021 08:31:30', '%m.%d.%Y %h:%i:%s'); -- 2021-08-31 08:09:30 6
可以看到,str_to_date(str,format) 转换函数,可以把一些杂乱无章的字符串转换为日期格式。另外,它也可以转换为时间。“format” 可以参看 MySQL 手册。
- (日期、天数)转换函数
MySQL (日期、天数)转换函数:to_days(date), from_days(days)
1select to_days('0000-00-00'); -- 0 2select to_days('2021-08-31'); -- 738398
- (时间、秒)转换函数
MySQL (时间、秒)转换函数:time_to_sec(time), sec_to_time(seconds)
1select time_to_sec('01:00:05'); -- 3605 2select sec_to_time(3605); -- '01:00:05'
- 拼凑日期、时间函数
MySQL 拼凑日期、时间函数:makdedate(year,dayofyear), maketime(hour,minute,second)
1select makedate(2021,31); -- '2021-01-31' 2select makedate(2021,32); -- '2021-02-01' 3select maketime(12,15,30); -- '12:15:30'
makedate(2021,31)前面是年份,后面是天数,默认重1月份开始,如果后面天数是多少天,就进一个月数,
- Unix 时间戳、日期 转换函数
unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)
1select unix_timestamp(); -- 1218290027 2select unix_timestamp('2008-08-08'); -- 1218124800 3select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800 4 5select from_unixtime(1218290027); -- '2008-08-09 21:53:47' 6select from_unixtime(1218124800); -- '2008-08-08 00:00:00' 7select from_unixtime(1218169800); -- '2008-08-08 12:30:00' 8 9select from_unixtime(1218169800, '%Y %D %M %h:%i:%s %x'); -- '2008 8th August 12:30:00 2008'
mysql 日期时间计算函数
- 为日期增加一个时间间隔:
date_add()
1set @dt = now(); -- 定义变量 2 3select date_add(@dt, interval 1 day); -- add 1 day 4select date_add(@dt, interval 1 hour); -- add 1 hour 5select date_add(@dt, interval 1 minute); -- ... 6select date_add(@dt, interval 1 second); 7select date_add(@dt, interval 1 microsecond); 8select date_add(@dt, interval 1 week); 9select date_add(@dt, interval 1 month); 10select date_add(@dt, interval 1 quarter); 11select date_add(@dt, interval 1 year); 12 13select date_add(@dt, interval -1 day); -- sub 1 day 加-1天也就是减1天
MySQL adddate(), addtime()函数,可以用 date_add() 来替代。下面是 date_add() 实现 addtime() 功能示例:
1set @dt = '2021-08-31 12:12:33'; 2select date_add(@dt, interval '01:15:30' hour_second); -- 2021-08-31 13:28:03 3select date_add(@dt, interval '1 01:15:30' day_second); -- 2021-09-01 13:28:03
- 为日期减去一个时间间隔:
date_sub()
1-- 2021-08-29 22:58:59 2select date_sub('2021-08-31 00:00:00', interval '1 1:1:1' day_second);
date_sub() 日期时间函数 和 date_add() 用法一致,不再赘述。
- 日期、时间相减函数:
datediff(date1,date2),timediff(time1,time2)
1-- 两个日期相减 date1 - date2,返回天数。 2select datediff('2021-08-31', '2021-08-30'); -- 1 3select datediff('2021-08-31', '2021-08-20'); -- -11
1-- 两个日期相减 time1 - time2,返回 time 差值。 2select timediff('2021-08-31 08:08:08', '2021-08-30 00:00:00'); -- 32:08:08 3select timediff('08:08:08', '00:00:00'); -- -08:08:08
timediff(time1,time2)函数的两个参数类型必须相同
- 时间戳(
timestamp)转换、增、减函数:
timestamp(date) -- date to timestamp
timestamp(dt,time) -- dt + time
timestampadd(unit,interval,datetime_expr) --
timestampdiff(unit,datetime_expr1,datetime_expr2) --
1select timestamp('2021-08-31'); -- 2021-08-31 00:00:00 2select timestamp('2021-08-31 08:00:00', '01:01:01'); -- 2021-08-31 09:01:01 3select timestamp('2021-08-31 08:00:00', '10 01:01:01'); -- 2021-09-10 09:01:01 4 5select timestampadd(day, 1, '2021-08-31 08:00:00'); -- 2021-09-01 08:00:00 6select date_add('2021-08-31 08:00:00', interval 1 day); -- 2021-09-01 08:00:00 7 8-- MySQL timestampadd() 函数类似于 date_add()。 9select timestampdiff(year,'2021-08-31','2001-01-01'); -- -20 10select timestampdiff(day ,'2021-08-31','2001-01-01'); -- --7547 11select timestampdiff(hour,'2021-08-31 12:00:00','2021-08-31 00:00:00'); -- -12 12 13select datediff('2021-08-31 12:00:00', '2021-08-31 00:00:00'); -- 0
MySQL
timestampdiff()函数就比datediff()功能强多了,datediff()只能计算两个日期(date)之间相差的天数
时区(timezone)转换函数
convert_tz(dt,from_tz,to_tz)函数
1-- 2021-08-31 04:00:00 2select convert_tz('2021-08-31 12:00:00', '+08:00', '+00:00');
时区转换也可以通过
date_add, date_sub, timestampadd来实现。
1select date_add('2021-08-31 12:00:00', interval -8 hour); -- 2021-08-31 04:00:00 2select date_sub('2021-08-31 12:00:00', interval 8 hour); -- 2021-08-31 04:00:00 3select timestampadd(hour, -8, '2021-08-31 12:00:00'); -- 2021-08-31 04:00:00
