SUBTIME(time,expr) 函数用于执行时间的减法运算,返回一个时间字符串,如:12:00:15。
time 是一个时间或日期时间表达式,如:2022-04-26 12:30:00 或者 12:30:00。
expr 是一个时间。如:00:30:00 或者 0:30:0
(1)使用 SUBTIME(time,expr) 函数对给定的时间字符串减去 30 分钟。如下:
mysql> select SUBTIME('2022-04-26 12:30:00', '00:30:00'); +--------------------------------------------+ | SUBTIME('2022-04-26 12:30:00', '00:30:00') | +--------------------------------------------+ | 2022-04-26 12:00:00 | +--------------------------------------------+ 1 row in set (0.02 sec) mysql> select SUBTIME('12:30:00', '00:30:00'); +---------------------------------+ | SUBTIME('12:30:00', '00:30:00') | +---------------------------------+ | 12:00:00 | +---------------------------------+ 1 row in set (0.03 sec) mysql> select SUBTIME('12:30:00', '0:30:0'); +-------------------------------+ | SUBTIME('12:30:00', '0:30:0') | +-------------------------------+ | 12:00:00 | +-------------------------------+ 1 row in set (0.03 sec)
(2)使用 SUBTIME(time,expr) 函数将当前日期减去 30 分钟。如下:
mysql> select NOW(),SUBTIME(NOW(), '00:30:00'); +---------------------+----------------------------+ | NOW() | SUBTIME(NOW(), '00:30:00') | +---------------------+----------------------------+ | 2022-04-26 11:50:17 | 2022-04-26 11:20:17 | +---------------------+----------------------------+ 1 row in set (0.04 sec)
(3)使用 SUBTIME(time,expr) 函数时,如果任意一个参数为NULL,或者为空字符串,直接返回 NULL。如下:
mysql> select SUBTIME(NULL, '00:30:00'); +---------------------------+ | SUBTIME(NULL, '00:30:00') | +---------------------------+ | NULL | +---------------------------+ 1 row in set (0.02 sec) mysql> select SUBTIME(NOW(), NULL); +----------------------+ | SUBTIME(NOW(), NULL) | +----------------------+ | NULL | +----------------------+ 1 row in set (0.02 sec) mysql> select SUBTIME('', '00:30:00'); +-------------------------+ | SUBTIME('', '00:30:00') | +-------------------------+ | NULL | +-------------------------+ 1 row in set (0.03 sec) mysql> select SUBTIME(NOW(), ''); +--------------------+ | SUBTIME(NOW(), '') | +--------------------+ | NULL | +--------------------+ 1 row in set (0.03 sec)