Mysql中日期和时间计算

2019-03-10  本文已影响0人  曼木子

mysql中的日期和时间类型

类型 字节 最小值 最大值
date 4字节 1000-01-01 9999-12-31
datetime 8字节 1000-01-01 00:00:00 9999-12-31 23:59:59
timestamp 4字节 19700101080001 2038年的某个时刻
time 3字节 -835:59:59 838:59:59
year 1字节 1901 2155

mysql中的日期函数

curdate() :当前日期
now():当前日期时间
time():当前时间
year():当前年

时间差值,并表示为天、小时、分钟、秒

TIMESTAMPDIFF( second,startDate , endDate )

SELECT TIMESTAMPDIFF(SECOND,'2019-03-02 00:00:00','2019-03-02 00:10:10')
//结果为610秒
mysql中between and 不包含右边边界
select * from 表
where data between '2019-01-01' and '2019-01-05'
//查询的结果没有date为2019-01-05的数据

时间差转为天、小时、分钟、秒
floor()向下取整
select
time_diff,//计算的时间差
floor(time_diff / 86400) as '天',
floor(time_diff % 86400 / 3600) as '时',
floor(time_diff % 3600 / 60)  as '分',
floor(time_diff % 60) as '秒'
上一篇 下一篇

猜你喜欢

热点阅读