从时间值中减去分钟数

时间:2012-07-12 08:25:23

标签: mysql

我的数据库中有一个时间字段,其值为22:05

我想减去5分钟。当字段是datetime时我使用sub_date并且工作正常,但我无法通过时间字段得到它,我收到警告我不知道如何处理;

mysql>  SELECT scheduleFinalTime FROM clientSchedule WHERE clientScheduleID = 3;
+-------------------+
| scheduleFinalTime |
+-------------------+
| 22:05:00          |
+-------------------+
1 row in set (0.00 sec)

mysql> SELECT date_sub(scheduleFinalTime, INTERVAL 5 MINUTE) FROM clientSchedule WHERE clientScheduleID = 3;
+------------------------------------------------+
| date_sub(scheduleFinalTime, INTERVAL 5 MINUTE) |
+------------------------------------------------+
| NULL                                           |
+------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+------------------------------------------------------------+
| Level   | Code | Message                                                    |
+---------+------+------------------------------------------------------------+
| Warning | 1264 | Out of range value for column 'scheduleFinalTime' at row 1 |
+---------+------+------------------------------------------------------------+
 1 row in set (0.00 sec)

提前谢谢

2 个答案:

答案 0 :(得分:9)

Mysql有一个SUBTIME()函数。

SUBTIME(expr1,expr2)

SUBTIME() returns expr1 − expr2 expressed as a value in the same format as expr1. expr1 is a time or datetime expression, and expr2 is a time expression.

mysql> SELECT SUBTIME('2007-12-31 23:59:59.999999','1 1:1:1.000002');
        -> '2007-12-30 22:58:58.999997'
mysql> SELECT SUBTIME('01:00:00.999999', '02:00:00.999998');
        -> '-00:59:59.999999'

答案 1 :(得分:2)

对于那些使用mysql 4.x

的人
SEC_TO_TIME(TIME_TO_SEC(t2) - TIME_TO_SEC(t1))