mysql Timediff()函数返回0.0000

时间:2013-06-24 20:05:29

标签: mysql sql sql-server

在Sql Server中:

declare @totalUsageInSeconds decimal(15,6)
SELECT @totalUsageInSeconds = @totalUsageInSeconds + DATEDIFF(SECOND, @resourceStartTime, @resourceEndTime)

以上查询以秒为单位给出了正确的日期差异

但是在Mysql中,差异是秒为0.0000 :(请参阅下面的内容)

declare _totalUsageInSeconds decimal(15,6);
SELECT @totalUsageInSeconds = @totalUsageInSeconds + TIME_TO_SEC(TIMEDIFF(_resourceStartTime, _resourceEndTime));

我可以知道原因以及如何解决问题吗?

1 个答案:

答案 0 :(得分:1)

声明变量时,它设置为NULL。当您向NULL添加任何内容时,您会获得NULL

更改此行:

declare @totalUsageInSeconds decimal(15,6) = 0;

或添加:

select @totalUsageInSeconds = 0