我在 MySQL 表中有 2 个 datetime
列。
我想减去它并返回结果和纳秒精度
到目前为止,这是我的代码。
select end,start,end-start from job where id=1;
我有这样的结果
'2014-04-02 12:30:00', '2014-04-02 10:30:00', 20000.000000
好奇MySQL是怎么直接减的,20000.000000
是怎么来的。
我不擅长处理日期。哪种方法最好?
最佳答案
unix_timestamp : 如果你想将 UNIX TIMESTAMP 转换为自 '1970-01-01' 以来的秒数
If called with no argument, returns a Unix timestamp (seconds since '1970-01-01 00:00:00' UTC) as an unsigned integer. If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' UTC. date may be a DATE string, a DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or YYYYMMDD.
考虑一下:
SELECT unix_timestamp(now()) - unix_timestamp('2007-03-19 09:50:00')
你想要这个:
SELECT unix_timestamp(column1) - unix_timestamp(column2) from tableName
SELECT start_time,
end_time,
( Unix_timestamp(start_time) - Unix_timestamp(end_time) ) * 1000000000 AS
NanoSeconds
FROM job
输出:
start_time | end_time | NanoSeconds
April, 02 2014 12:30:00 April, 02 2014 10:30:00 7200000000000
因为我们也可以使用 DATE_FORMAT()
格式化此日期时间列值。 TO_SECONDS()从 MySQL 5.5.0 开始可用。
MYSQL - datetime to seconds还可以轻松了解您想要的输出的另一种解决方案。
关于MySQL 减去两个日期时间并以纳秒为单位返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30416979/