Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select milliseconds passed between datetime and now(3)

Tags:

sql

mysql

I am looking for a way to extract difference in milliseconds between two date. One date is stored, and is a date of a particular event. Second date is the result of now(3) statement. I need to store also milliseconds.

Until now, I've discovered timediff function. But this returns something like HH:ii:ss.mm. I need, in SQL to convert this result in milliseconds.

mysql> select timediff(now(3), updated_at) from events;
+------------------------------+
| timediff(now(3), updated_at) |
+------------------------------+
| 00:42:22.240                 |
+------------------------------+
1 row in set (0,01 sec)
like image 495
sensorario Avatar asked Mar 08 '26 22:03

sensorario


1 Answers

MySQL only supports microsecond for a higher resolution, but you can achieve it by converting microsecond to millisecond manually:

SELECT TIMESTAMPDIFF(MICROSECOND, now(3), updated_at) / 1000 AS diff_in_ms FROM events;
like image 100
walter Avatar answered Mar 11 '26 13:03

walter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!