CURRENT_TIMESTAMP in milliseconds

后端 未结 19 915
失恋的感觉
失恋的感觉 2020-12-07 23:48

Is there any way to get milliseconds out of a timestamp in MySql or PostgreSql (or others just out of curiosity)?

SELECT CURRENT_TI         


        
19条回答
  •  一生所求
    2020-12-08 00:30

    Poster is asking for an integer value of MS since Epoch, not a time or S since Epoch.

    For that, you need to use NOW(3) which gives you time in fractional seconds to 3 decimal places (ie MS precision): 2020-02-13 16:30:18.236

    Then UNIX_TIMESTAMP(NOW(3)) to get the time to fractional seconds since epoc: 1581611418.236

    Finally, FLOOR(UNIX_TIMESTAMP(NOW(3))*1000) to get it to a nice round integer, for ms since epoc: 1581611418236

    Make it a MySQL Function:

    CREATE FUNCTION UNIX_MS() RETURN BIGINT DETERMINISTIC
    BEGIN
        RETURN FLOOR(UNIX_TIMESTAMP(NOW(3))*1000);
    END
    

    Now run SELECT UNIX_MS();

    Note: this was all copied by hand so if there are mistakes feel free to fix ;)

提交回复
热议问题