Following 2 queries should calculate the difference in minutes, but one is correct and the other gives a wrong result. Is this a bug, or my bug?
SELECT FLOOR(TIME_TO_SEC(TIMEDIFF(NOW(),start_time))/60) AS minutes = 23 (correct)
SELECT TIME_TO_SEC(TIMEDIFF(NOW(),start_time)) DIV 60 AS minutes = -50339 (wrong)
Probably I prefer DIV, but it doesn’t work properly.
I am using Server version: 10.11.8-MariaDB-0ubuntu0.24.04.1, in Ubuntu 24.
2
Answers
I don’t think that this is a bug. See MariaDB SkySQL documentation for DIV:
I.e. it is expected for
FLOOR
andDIV
to give different results when the operand is negative.You might try
Or take the absolute value with
ABS
.Also note that
TIME_TO_SEC
returnsDOUBLE
notINT
.DIV
:By contrast,
FLOOR
expects non-integer arguments.TIMESTAMPDIFF() function
PS. When precise result needs in rounding then decimal part is simply truncated.
I.e. you need in trivial