如何在MySQL中将TIME和DATETIME值转换为数字形式?

可以将TIME(N)和DATETIME(N)值转换为数字形式,方法是将它们添加0(+0)。以下是这种转换的规则-

转换为INTEGER

当N为0时,TIME(N)和DATETIME(N)值将转换为整数。

例如,值CURTIME()NOW()可以转换为整数值如下-

mysql> SELECT CURTIME(), CURTIME()+0;
+-----------+-------------------+
| CURTIME() | CURTIME()+0       |
+-----------+-------------------+
| 19:42:54  | 194254            |
+-----------+-------------------+
1 row in set (0.04 sec)

mysql> SELECT NOW(), NOW()+0;
+-------------------------+----------------------------------+
| NOW()                   | NOW()+0                          |
+-------------------------+----------------------------------+
| 2017-10-27 19:43:43     | 20171027194343                   |
+-------------------------+----------------------------------+
1 row in set (0.00 sec)

转换为DECIMAL

的时间(N)和DATETIME(N)的值将被转换为整数时,N是大于0
例如,的值CURTIME()NOW()可如下转化成十进制值-

mysql> SELECT CURTIME(), CURTIME(3)+0;
+-----------+-------------------+
| CURTIME() | CURTIME()+0       |
+-----------+-------------------+
| 19:47:40  | 194740.575        |
+-----------+-------------------+
1 row in set (0.04 sec)

mysql> SELECT NOW(), NOW(3)+0;
+-------------------------+----------------------------------+
| NOW()                   | NOW()+0                          |
+-------------------------+----------------------------------+
| 2017-10-27 19:48:45     | 20171027194845.589               |
+-------------------------+----------------------------------+
1 row in set (0.00 sec)
猜你喜欢