How to compare milliseconds in MySQL with a given date

房东的猫 提交于 2019-12-24 02:14:32

问题


Using

mysql  Ver 14.12 Distrib 5.0.45, for redhat-linux-gnu (i686) using readline 5.0

I have a table defined like this:

+-----------------------+--------------+------+-----+---------+-------+
| Field                 | Type         | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+-------+
| id                    | bigint(20)   | NO   | PRI |         |       | 
| user_id               | bigint(20)   | NO   | MUL |         |       | 
| directory_id          | bigint(20)   | NO   | MUL |         |       | 
| attribute_name        | varchar(255) | NO   |     |         |       | 
| attribute_value       | varchar(255) | YES  |     | NULL    |       | 
| attribute_lower_value | varchar(255) | YES  |     | NULL    |       | 
+-----------------------+--------------+------+-----+---------+-------+

Selecting the attribute_value for my last authenticated time returns

+-------------------+-----------------+
| attribute_name    | attribute_value |
+-------------------+-----------------+
| lastAuthenticated | 1330380013284   | 
+-------------------+-----------------+

If I verify the value using http://www.epochconverter.com/ it says

Assuming that this timestamp is in milliseconds:
GMT: Mon, 27 Feb 2012 22:00:13 GMT

But if I try the following line of code

mysql> select from_unixtime('1330380013284');
+--------------------------------+
| from_unixtime('1330380013284') |
+--------------------------------+
| NULL                           | 
+--------------------------------+

Anyone who immediately knows what I'm missing here?


回答1:


You need to divide by 1000 as well as remove the quotes, otherwise your dates are way off

SELECT FROM_UNIXTIME(theField/1000);

SELECT FROM_UNIXTIME(1330380013284/1000);

See the documentation - notice the MySQL methods return SECONDS, not milliseconds like you are using:

MySQL docs for UNIX_TIMESTAMP

If called with no argument, returns a Unix timestamp 
(SECONDS since '1970-01-01 00:00:00' UTC) as an unsigned integer



回答2:


Remove the ', you have to pass a number.



来源:https://stackoverflow.com/questions/9483205/how-to-compare-milliseconds-in-mysql-with-a-given-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!