Mysql 日期格式化及复杂日期区间查询
16lz
2021-12-09
前言
最近在做项目涉及到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()
select * from t_user t where DATE (t.CREATE_TIME)>=DATE (now())
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 |+---------------------+---------------------+
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 |+----------------------------------------------------+
2 字符串转换为日期时间
MySQL Str to Date (字符串转换为日期)函数:str_to_date(str, format)
select str_to_date('08/31/2021', '%m/%d/%Y'); -- 2021-08-31select str_to_date('08/31/2021' , '%m/%d/%y'); -- 2021-08-31select str_to_date('08.31.2021', '%m.%d.%Y'); -- 2021-08-31select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30select str_to_date('08.09.2021 08:31:30', '%m.%d.%Y %h:%i:%s'); -- 2021-08-31 08:09:30
3 (日期、天数)转换函数
MySQL (日期、天数)转换函数:to_days(date), from_days(days)
select to_days('0000-00-00'); -- 0select to_days('2021-08-31'); -- 738398
select time_to_sec('01:00:05'); -- 3605select sec_to_time(3605); -- '01:00:05'
select makedate(2021,31); -- '2021-01-31'select makedate(2021,32); -- '2021-02-01'select maketime(12,15,30); -- '12:15:30'
6 Unix 时间戳、日期 转换函数
unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)
select unix_timestamp(); -- 1218290027select unix_timestamp('2008-08-08'); -- 1218124800select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800select 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'
set @dt = '2021-08-31 12:12:33';select date_add(@dt, interval '01:15:30' hour_second); -- 2021-08-31 13:28:03select date_add(@dt, interval '1 01:15:30' day_second); -- 2021-09-01 13:28:03
3 日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)
-- 两个日期相减 date1 - date2,返回天数。select datediff('2021-08-31', '2021-08-30'); -- 1select 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:08select timediff('08:08:08', '00:00:00'); -- -08:08:08
4 时间戳(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:00select timestamp('2021-08-31 08:00:00', '01:01:01'); -- 2021-08-31 09:01:01select timestamp('2021-08-31 08:00:00', '10 01:01:01'); -- 2021-09-10 09:01:01select timestampadd(day, 1, '2021-08-31 08:00:00'); -- 2021-09-01 08:00:00select 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'); -- -20select timestampdiff(day ,'2021-08-31','2001-01-01'); -- --7547select timestampdiff(hour,'2021-08-31 12:00:00','2021-08-31 00:00:00'); -- -12select datediff('2021-08-31 12:00:00', '2021-08-31 00:00:00'); -- 0
时区(timezone)转换函数
convert_tz(dt,from_tz,to_tz) 函数
-- 2021-08-31 04:00:00select convert_tz('2021-08-31 12:00:00', '+08:00', '+00:00');
更多相关文章
- Android(安卓)Calendar使用过程中遇到的问题
- DatePicker 日期选择控件 DatePickerDialog 日期选择对话框
- android 日期对话框
- android calendar的使用
- android 内存缓冲机制:MemoryCache
- ch07 Android(安卓)日期与时间对话框
- ch07 Android(安卓)日期与时间对话框
- Android时间工具类 本地转UTC,UTC转本地
- Android中实现日期时间选择器(DatePicker和TimePicker)