I am trying to create my first stored function on MySQL. In this function I want to return the timestamp of the current date and time with 3 microsecond digits like this:
Also you can
mysql> select now(3) as millisecond, now(6) as microsecond, round(1000 * unix_timestamp(now(3))) elapsed_millisecond, round(unix_timestamp() * 1000 + MICROSECOND(now(6)) / 1000) elapsed_microsecond;
+-------------------------+----------------------------+---------------------+---------------------+
| millisecond | microsecond | elapsed_millisecond | elapsed_microsecond |
+-------------------------+----------------------------+---------------------+---------------------+
| 2019-12-10 11:49:43.568 | 2019-12-10 11:49:43.568767 | 1575949783568 | 1575949783569 |
+-------------------------+----------------------------+---------------------+---------------------+
1 row in set (0.01 sec)
This is my Solution for Millisecond management.
I just use "text" data type as data store, of cause you have to control data validation by yourself.
CREATE TABLE time_test
(
id
INT NOT NULL AUTO_INCREMENT ,
start_time
TEXT NULL ,
stop_time
TEXT NULL ,
difference
TEXT NULL ,
ranking
INT NULL ,
PRIMARY KEY ( id
)
)
INSERT INTO time_test
VALUES (NULL, '10:10:10.111111', '10:10:10.456789',NULL,NULL)
INSERT INTO time_test
VALUES (NULL, '10:10:10.111111', '10:10:20.777777',NULL,NULL)
INSERT INTO time_test
VALUES (NULL, '10:10:10.111111', '10:10:01.999999',NULL,NULL)
Now you can calculate like this
Now you can calculate like this
sorting
SELECT * FROM time_test
ORDER BY TIME( stop_time )
Calculate time diff, very good for me.
SELECT *, TIMEDIFF(stop_time,start_time) from time_test
Calculate time diff, very good for me.
also can calcuate and store back you can cut some string by yourself.
update time_test
set difference = concat(TIMEDIFF(stop_time,start_time), MICROSECOND(TIMEDIFF(stop_time,start_time) ))
also can calcuate and store back you can cut some string by yourself.
you also doing ranking by this command:
SET @r:=0;
UPDATE time_test
SET ranking= (@r:= (@r+1)) ORDER BY difference
ASC;
you also doing ranking by this command:
For mysql 5.6
round(unix_timestamp() * 1000 + MICROSECOND(sysdate(6)) / 1000)
MySQL 5.6 supports the millisecond precision in the sysdate function.
try
select sysdate(6)
will return 2013-04-16 13:47:56.273434
and
select sysdate(3)
will return 2013-04-16 13:47:56.273
Take a look at what MySQL says(http://dev.mysql.com/doc/refman/5.1/en/fractional-seconds.html):
However, when MySQL stores a value into a column of any temporal data type, it discards any fractional part and does not store it.
So you need to store it not as a date value, but as a simple floating point value.