NOTE: This doesn’t generate a timestamp accurate to milliseconds. It just stores it in two fields. AFAIK there is no way to generate such a thing. It is accurately described here. Sorry for the confusion =)
Bug #8523 details MySQL’s deficiency in its ability to store and retrieve datetime values with millisecond or microsecond precision. When performance testing and perhaps storing trace logs or audit information in a MySQL table for later analysis, this shortfall is quite frustrating.
My hacky way to deal with this is to store the millisecond component as its own integer. Say for example you are interested in the difference between a Put datetime and Get datetime for MQ, your table might look like this:
CREATE TABLE `msecs` ( `put_datetime` datetime default NULL, `put_msecs` int(11) default NULL, `get_datetime` datetime default NULL, `get_msecs` int(11) default NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
So in this table I am storing the datetime component in its native MySQL format and the millisecond component as an integer. Here are some examples:

Now to retrieve and compare the values, I get the difference using TIME_TO_SEC for the datetime component, multiply it by 1000 to convert to milliseconds then add to the difference of the milliseconds component using basic maths:
SELECT ((TIME_TO_SEC(get_datetime) - TIME_TO_SEC(put_datetime))*1000) + (get_msecs-put_msecs) AS diff_msecs FROM msecs
That gives you difference in milliseconds for the demo table along these lines:
2300
1700
300
600700
700
Hopefully that makes sense. Pipe up if you have an easier solution!
ok but then how the heck are you getting the micromilli secs in your columns? Are you running a counter?
Yes, but this is only half of the problem (and the easiest half): the other is that the function NOW() doens’t retrieve the milli/micro seconds, but only seconds. So How you get the microseconds? There is now solution other than the UDF posted the in the bug thread.
This is the clsoest to a solution I have been able to find.
The next issue is once you have done your math comparisons, how do you convert the value back into Time and MSec fields in order to insert them back into the table?
@Jerry
Math works both ways. So if you have 2313ms coming in, we can do (pseudocode)
ms_in = 2313;
put_dt_ms = ms_in%1000;
put_dt = SEC_TO_TIME((ms_in-put_dt_ms)/1000);
-Joe
I want straight forward way to retrieve & insert the time in milliseconds
What I think is an easier solution is to use decimal(17,3) to store the timestamp including the millisecond value in a single column as shown here: http://mysqlpreacher.com/wordpress/2009/08/once-upon-a-timestampmilliseconds/
Generating a millisecond value can be done using UDFs: http://bugs.mysql.com/bug.php?id=8523
Darren
I know this post is old, but why do you need 11 digits for the milliseconds? smallint(3) should be enough.