如何从 MySQL DATETIME 实例中获取总秒数?
MySQL DateTime 实例可以使用 UNIX_TIMESTAMP()函数以以下方式转换为秒 -
mysql> Select UNIX_TIMESTAMP('2017-05-15 04:05:30') AS 'NUMBER OF SECONDS'; +-------------------+ | NUMBER OF SECONDS | +-------------------+ | 1494801330 | +-------------------+ 1 row in set (0.00 sec)
上述查询会将给定的 datetime 实例转换为总秒数。
mysql> Select UNIX_TIMESTAMP(NOW()) AS 'NUMBER OF SECONDS'; +-------------------+ | NUMBER OF SECONDS | +-------------------+ | 1509248856 | +-------------------+ 1 row in set (0.00 sec)
上述查询会将当前 DateTime 实例转换为总秒数。
mysql> Select UNIX_TIMESTAMP(Dateofreg) AS 'NUMBER OF SECONDS' from testing where StudentName = 'Gaurav'; +-------------------+ | NUMBER OF SECONDS | +-------------------+ | 1509247113 | +-------------------+ 1 row in set (0.00 sec)
上述查询会转换 'testing' 表中存储的值为 DateTime 实例,其中 StudentName 列的值为 'Gaurav'。
Advertisement