MariaDB转换created_at时间戳(从现在开始的总小时数)

ws51t4hk  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(160)

我有以下问题返回created_at时间戳。我想从现在开始将其转换为总小时数。有没有简单的方法进行转换并以总小时数打印它?
MariaDB版本10.5.12-MariaDB-1:10.5.12+maria~focal-log

MariaDB [nova]> select hostname, uuid, instances.created_at, instances.deleted_at, json_extract(flavor, '$.cur.*."name"') AS FLAVOR from instances join instance_extra on instances.uuid = instance_extra.instance_uuid WHERE (vm_state='active' OR vm_state='stopped');
+----------+--------------------------------------+---------------------+------------+--------------+
| hostname | uuid                                 | created_at          | deleted_at | FLAVOR       |
+----------+--------------------------------------+---------------------+------------+--------------+
| vm1      | ef6380b4-5455-48f8-9e4b-3d04199be3f5 | 2023-01-05 14:25:51 | NULL       | ["tempest2"] |
+----------+--------------------------------------+---------------------+------------+--------------+
1 row in set (0.001 sec)
new9mtju

new9mtju1#

试着这样做:

SELECT hostname, UUID, instances.created_at, 
       TIMESTAMPDIFF(hour,instances.created_at, NOW()) AS HOURDIFF,
       instances.deleted_at, 
       JSON_EXTRACT(flavor, '$.cur.*."name"') AS FLAVOR 
FROM instances 
JOIN instance_extra ON instances.uuid = instance_extra.instance_uuid 
WHERE (vm_state='active' OR vm_state='stopped');

Demo fiddle

相关问题