K
K
khodos_dmitry2018-02-27 20:17:53
MySQL
khodos_dmitry, 2018-02-27 20:17:53

How to insert current time in milliseconds into mysql?

`updated` = NOW() + 0 doesn't work for me for some reason

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
ivankomolin, 2018-02-27
@khodos_dmitry

If you have mysql 5.6 and above use this:
If below, you will have to set it via php.
A little theory:

mysql> CREATE TABLE fractest( c1 TIME(2), c2 DATETIME(2), c3 TIMESTAMP(2) );
Query OK, 0 rows affected (0.33 sec)

mysql> INSERT INTO fractest VALUES
     > ('17:51:04.777', '2014-09-08 17:51:04.777', '2014-09-08 17:51:04.777');
Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM fractest;
+-------------+------------------------+------------------------+
| c1          | c2                     | c3                     |
+-------------+------------------------+------------------------+
| 17:51:04.78 | 2014-09-08 17:51:04.78 | 2014-09-08 17:51:04.78 |
+-------------+------------------------+------------------------+
1 row in set (0.00 sec)

Notice how the TIME(2) data types are defined. The number in brackets means the number of characters after the dot in the total. Up to 6 digits (microseconds) are supported.
It is also possible to get the current date with microsecond precision, example:
This works since mysql 5.6.
In mysql 5.5, the date must be stored in the same format up to 6 digits after the dot as a string, it is impossible to get the current date with an accuracy greater than seconds. The only thing in this direction is the MICROSECOND function. It allows you to extract the fractional part from a string with a date.
SELECT MICROSECOND('2010-12-10 14:12:09.019473');

O
Oleg, 2018-02-27
@402d

Read about field types.
we understand that standard types do not work more precisely than seconds. write our implementation
1. store in BIGINT
2. convert to "normal" time
select FROM_UNIXTIME(datefield/1000) from my_table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question