前言
最近在做项目涉及到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日期时间函数
- 得当前日期+时间(date + time)函数:
now()
select now();
+---------------------+
| now() |
+---------------------+
| 2021-08-31 16:16:32 |
+---------------------+
- 获得当前日期+时间(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() 日期时间函数,一般情况下很少用到。
- 获得当前时间戳函数:
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日期时间转换函数
- 日期时间转换字符串格式
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) 函数的 一个逆转换。
- 字符串转换为日期时间
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 手册。
- (日期、天数)转换函数
MySQL (日期、天数)转换函数:to_days(date), from_days(days)
select to_days(\'0000-00-00\'); -- 0
select to_days(\'2021-08-31\'); -- 738398
- (时间、秒)转换函数
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\'
- 拼凑日期、时间函数
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月份开始,如果后面天数是多少天,就进一个月数,
- 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 日期时间计算函数
- 为日期增加一个时间间隔:
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 adddate(), addtime()函数,可以用 date_add() 来替代。下面是 date_add() 实现 addtime() 功能示例:
set @dt = \'2021-08-31 12:12:33\';
select date_add(@dt, interval \'01:15:30\' hour_second); -- 2021-08-31 13:28:03
select date_add(@dt, interval \'1 01:15:30\' day_second); -- 2021-09-01 13:28:03
- 为日期减去一个时间间隔:
date_sub()
-- 2021-08-29 22:58:59
select 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)
-- 两个日期相减 date1 - date2,返回天数。
select datediff(\'2021-08-31\', \'2021-08-30\'); -- 1
select datediff(\'2021-08-31\', \'2021-08-20\'); -- -11
-- 两个日期相减 time1 - time2,返回 time 差值。
select timediff(\'2021-08-31 08:08:08\', \'2021-08-30 00:00:00\'); -- 32:08:08
select 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) --
select timestamp(\'2021-08-31\'); -- 2021-08-31 00:00:00
select timestamp(\'2021-08-31 08:00:00\', \'01:01:01\'); -- 2021-08-31 09:01:01
select timestamp(\'2021-08-31 08:00:00\', \'10 01:01:01\'); -- 2021-09-10 09:01:01
select timestampadd(day, 1, \'2021-08-31 08:00:00\'); -- 2021-09-01 08:00:00
select date_add(\'2021-08-31 08:00:00\', interval 1 day); -- 2021-09-01 08:00:00
-- MySQL timestampadd() 函数类似于 date_add()。
select timestampdiff(year,\'2021-08-31\',\'2001-01-01\'); -- -20
select timestampdiff(day ,\'2021-08-31\',\'2001-01-01\'); -- --7547
select timestampdiff(hour,\'2021-08-31 12:00:00\',\'2021-08-31 00:00:00\'); -- -12
select 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)函数
-- 2021-08-31 04:00:00
select convert_tz(\'2021-08-31 12:00:00\', \'+08:00\', \'+00:00\');
时区转换也可以通过
date_add, date_sub, timestampadd来实现。
select date_add(\'2021-08-31 12:00:00\', interval -8 hour); -- 2021-08-31 04:00:00
select date_sub(\'2021-08-31 12:00:00\', interval 8 hour); -- 2021-08-31 04:00:00
select timestampadd(hour, -8, \'2021-08-31 12:00:00\'); -- 2021-08-31 04:00:00