How to insert time 2009-09-22 18:09:37.881 in MYSQL My column type is DateTime

前端 未结 5 1337
野趣味
野趣味 2021-01-06 00:57

How to insert time 2009-09-22 18:09:37.881 in mysql. Actually I can insert and retrieve the time 2009-09-22 18:09:37 in mysql but whenever I am try

相关标签:
5条回答
  • 2021-01-06 01:05

    The fractional second capability was finally added in MySQL 5.6.4. Link to documentaion: http://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html

    0 讨论(0)
  • 2021-01-06 01:13

    All time types in MySQL have only a precision of seconds

    [...] microseconds cannot be stored into a column of any temporal data type. Any microseconds part is discarded.

    You'll need some workaround.

    0 讨论(0)
  • 2021-01-06 01:27

    I got the issue resolved. The database was not allowing the insertion of time in millisecond. Please have a look at the lines, below:

    CREATE TABLE MyTimeStamp(TimeData decimal(17,3));
    
    INSERT INTO  MyTimeStamp(TimeData) values (20090922201843.426);
    
    SELECT timestamp(TimeData) FROM MyTimeStamp;
    

    OutPut:

    2009-09-22 20:018:43.426000
    
    0 讨论(0)
  • 2021-01-06 01:27

    Having had a quick scan on the MySQL reference pages here, they seem to suggest that you cannot add milliseconds to a column of type datetime. The direct quote is

    "However, microseconds cannot be stored into a column of any temporal data type. Any microseconds part is discarded."

    This implies that you cannot use Timestamp either.

    Are the miliseconds necessary? Could they be stored in a secondary column and then recombined with the basic datetime upon retrieval?

    0 讨论(0)
  • 2021-01-06 01:29
    <?
    # PHP
    $microtime = microtime(true);
    $datetime = date("Y-m-d H:i:s");
    ?>
    

    MYSQL

    • CREATE field col_datetime - DATETIME)
    • CREATE field col_microtime - DECIMAL(15,4)

    ADD: INSERT INTO your_table (col_datetime,col_microtime) values ('$datetime','$microtime')

    List new->old: SELECT * FROM yout_table ORDER BY col_microtime DESC

    List old->new: SELECT * FROM yout_table ORDER BY col_microtime

    0 讨论(0)
提交回复
热议问题