如何从MySQL DATETIME实例获取总秒数?
可以使用UNIX_TIMESTAMP()函数,通过以下方式将MySQLDateTime实例转换为秒:
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)
上面的查询将转换DateTime实例,该实例是表“testing”中存储值的值,其中StudentName列的值为“Gaurav”。