1.获取当前时间
SELECT NOW() AS nowTime; =》2021-03-12 18:00:03
2.获取天数的时间差
(1) SELECT TO_DAYS(NOW()) - TO_DAYS('20210310') AS days; =》2
(2) SELECT DATEDIFF(NOW(),'20210310') AS days;
3.获取时分秒、周、月、年等时间差
SELECT TIMESTAMPDIFF(WEEK,'2021-03-02','2021-03-16') AS weeks; =》获取周数 2
SELECT TIMESTAMPDIFF(MINUTE,'2021-03-10','2021-03-10 01:00:00') AS minutes; =》获取分钟 60
SELECT TIMESTAMPDIFF(SECOND,'2021-03-05 16:00:00','2021-03-05 16:10:00') AS seconds; =》获取秒数 60
其他还可以查询
FRAC_SECOND 表示间隔是毫秒
SECOND 秒
MINUTE 分钟
HOUR 小时
DAY 天
WEEK 星期
MONTH 月
QUARTER 季度
YEAR 年