How to retrieve microseconds or milliseconds from MySQL current time?

前端 未结 5 2158
天涯浪人
天涯浪人 2021-01-02 02:17

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:

5条回答
  •  误落风尘
    2021-01-02 02:38

    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:

提交回复
热议问题